我正在尝试这样做: | 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/