Chi*_*imu 32 android android-relativelayout
我在Android中以编程方式创建视图.父布局是RelativLlayout,我在其中添加两个视图.第一个是ListView第二个是自定义视图.我希望我的第二个自定义视图出现在列表的顶部,但不知何故,它隐藏在后面ListView.如何确保我的自定义视图位于顶部.
小智 32
视图按照将它们添加到父视图的顺序放置.最后添加的视图将位于顶部.您可能还想尝试
View.bringToFront() http://developer.android.com/reference/android/view/View.html#bringToFront()
解决方案1 -
View.bringToFront()如果使用正确,View#bringToFront()应该可以正常工作.也许你忘了这个(来源:http://developer.android.com/reference/android/view/View.html#bringToFront()):
在KITKAT之前,此方法之后应该调用requestLayout()和invalidate()
解决方案2 - 重新附加视图
如果View.bringToFront()不起作用,请尝试删除并再次添加视图.优点是它在预KITKAT版本上也有相同的代码.
我在我的代码中这样做:
ViewGroup parentView = (ViewGroup) (listView.getParent());
parentView.removeView(addFab);
parentView.addView(addFab);
Run Code Online (Sandbox Code Playgroud)
试试这个 :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<YourCustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)