[英]How to keep a View on top of my Fragments
听起来像是一个以前已经回答过的问题,但是从我的研究中我找不到解决方案。 我的布局大致如下:
主要区域是我将在其中添加/替换片段的容器,底部是我放置底部导航菜单的容器。 粉红色的“视图”是我需要放在菜单顶部的按钮(该按钮不起作用)。
每次将Fragment添加到主视图时,即使将TextView添加到容器View之后,“菜单”文本也会消失。 与粉红色的Button相同,该按钮添加在底部菜单容器之后(请参见下面的xml)。
如何将“菜单” TextView和粉红色的Button始终保留在片段顶部?
这是我的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:background="#eee"
tools:context="com.wecancer.wecancer.activities.Menu">
<FrameLayout
android:id="@+id/main_container_view"
android:layout_width="match_parent"
android:layout_height="520dp"
android:layout_marginBottom="0dp"
>
</FrameLayout>
<TextView
android:id="@+id/main_center_tv"
android:layout_centerVertical="true"
android:textSize="24sp"
android:layout_centerHorizontal="true"
android:text="@string/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_alignParentBottom="true"
android:id="@+id/main_bottom_menu_container"
android:layout_width="match_parent"
android:background="@color/lightGray"
android:layout_height="40dp"
>
</FrameLayout>
<Button
android:elevation="1dp"
android:id="@+id/menu_plus_img"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="@color/colorAccent"
android:layout_width="70dp"
android:layout_marginBottom="0dp"
android:layout_height="70dp"
tools:targetApi="lollipop" />
</RelativeLayout>
试试这个`
<FrameLayout
android:layout_alignParentBottom="true"
android:id="@+id/main_bottom_menu_container"
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="40dp"
>
</FrameLayout>
<Button
android:elevation="1dp"
android:id="@+id/menu_plus_img"
android:background="@color/colorAccent"
android:layout_width="70dp"
android:layout_height="70dp"
tools:targetApi="lollipop"
android:layout_marginLeft="71dp"
android:layout_marginStart="71dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/main_center_tv"
android:textSize="24sp"
android:text="@string/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_alignBaseline="@+id/menu_plus_img"
android:layout_alignBottom="@+id/menu_plus_img"
android:layout_toRightOf="@+id/menu_plus_img"
android:layout_toEndOf="@+id/menu_plus_img" />
<FrameLayout
android:id="@+id/main_container_view"
android:layout_width="match_parent"
android:layout_height="520dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/menu_plus_img">
</FrameLayout>
`
您只需要在使用Framelayout时更改布局中视图的顺序即可
布局的复杂性不需要RelativeLayout,因此您只需要切换即可。
我真的不知道为什么诸如putToFront , BringChildToFront或添加和删除View的子代以更改其索引的方法不起作用。 因此,我实际上必须创建一个新的布局结构。
代替:
RelativeLayout
...FrameLayout
...TextView
...FrameLayout
...Button
我做了:
FrameLayout
...RelativeLayout
......FrameLayout
......FrameLayout
......TextView
...RelativeLayout
......Button
而第二个RelativeLayout仅具有一个按钮和透明背景。 我的完整xml代码在下面作为参考。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_root_view"
android:layout_height="match_parent">
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
tools:context="com.wecancer.wecancer.activities.Menu">
<FrameLayout
android:layout_alignParentBottom="true"
android:id="@+id/main_bottom_menu_container"
android:layout_width="match_parent"
android:background="@color/lightGray"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/main_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp" />
<TextView
android:id="@+id/main_center_tv"
android:layout_centerVertical="true"
android:textSize="24sp"
android:layout_centerHorizontal="true"
android:text="@string/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:onClick="menuPlusBt"
android:elevation="5dp"
android:id="@+id/menu_plus_img"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:background="@drawable/green_circle"
android:layout_width="70dp"
android:layout_marginBottom="0dp"
android:layout_height="70dp"
tools:targetApi="lollipop" />
</RelativeLayout>
</FrameLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.