android - 在顶层保留一个具有半透明背景的 fragment

标签 android android-layout android-fragments

我正在尝试这样做: | fragment 菜单(半透明)| body(覆盖整个屏幕宽度)|

我在屏幕左侧的 fragment 中有一个菜单,当我单击菜单按钮时,我想在正文中打开一个新 fragment (正文的大小=屏幕的大小)。我希望 fragment 菜单必须在半透明背景下仍然可见。

这是我的 xml 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context="com.example.MainActivity" 
    android:background="@drawable/background_image"
    >

    <fragment android:name="com.example.Principal_Menu"
              android:id="@+id/menu_fragment"
              android:layout_width="150dp"
              android:layout_height="match_parent" />

    <FrameLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/body_container"
        android:orientation="vertical">

    </FrameLayout>

</RelativeLayout>

但是当我在 body_container 中替换或添加 fragment 时,menu_fragment 消失了:(
我希望我已经说清楚了,很抱歉提出了很多问题。

最佳答案

如果您希望透明菜单位于 body_container 前面,请像这样重新构建您的 xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity" 
android:background="@drawable/background_image" >

<FrameLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">    

  <FrameLayout 
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/body_container"
     android:orientation="vertical">

   </FrameLayout>

  <fragment android:name="com.example.Principal_Menu"
          android:id="@+id/menu_fragment"
          android:layout_width="150dp"
          android:layout_gravity="top|center_horizontal"
          android:layout_height="match_parent" />
</FrameLayout>

这里的逻辑是 FrameLayout 将 View 堆叠在 View 之上,因此使用此布局,body_container 中的 fragment 将位于菜单 fragment 后面,如果您希望 body_container 中的 fragment 位于前面,您可以在 fragment 上使用此代码getView().bringToFront(); 或调用此代码 getSupportFragmentManager().beginTransaction().hide(the_menu_fragment).commit();

希望对你有帮助

关于android - 在顶层保留一个具有半透明背景的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30314762/

相关文章:

android - 防止在 Android 应用程序代码中将号码更改为本地

android - 如何更改 Gradle 中的 libs 目录?

android - 在 ViewPager 滑动时显示/隐藏 FloatingActionButton

android - Android 中操作/标签栏的正确模式

java - 在 Android 中更改自定义 View 的宽度和高度

Android 多次使用相同的 fragment - 如何有不同的点击 Action

Android 模拟器可以正确显示希伯来语资源,但设备不能

android - 在运行时从自定义 ViewGroup 中的 TextView 类型的子项获取 "getText()"

android - Admob 横幅在多个 fragment 中的性能问题

java - 使用 Fragment 时找不到 id 的 View