我以编程方式在android中创建视图。父级布局是相对布局,我在其中添加了两个视图。首先是列表视图,其次是自定义视图。我希望我的第二个自定义视图位于列表视图的顶部,但不知怎的,它会隐藏在列表视图的后面。如何确保我的自定义视图处于顶层。如何在android中以编程方式将相对布局中的其他视图放在其他视图上?
回答
意见得到安置在将它们添加到他们的父母意见的顺序。最后添加的视图将位于顶部。你可能也想尝试 View.bringToFront()
http://developer.android.com/reference/android/view/View.html#bringToFront()
我尝试过使用bringToFront(),但它没有工作 – Chimu
试试这个:
<?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>
由于某种原因,我只想使用Relative布局,反正无法处理相对布局子的z顺序 – Chimu
还是那句话:浏览次数得到安置在您添加它的顺序布局。问题是:它是否真的隐藏或不呈现?检查LogCat和控制台是否有错误。如果您只将这一个自定义视图添加到您的布局,它是否呈现没有任何问题?如果是这样,请确保在添加ListView时真正将自定义添加到同一父视图(组)。如果您没有进一步了解,请提供相应的代码示例。
解决方案1 - 查看#bringToFront() View.bringToFront()
如果使用得当,应该可以正常工作。也许你忘了这(来源:http://developer.android.com/reference/android/view/View.html#bringToFront()):
此前奇巧应遵循这个方法通过调用requestLayout()和无效()
解决方案2 - 重新连接视图
如果View.bringToFront()
不起作用,请尝试删除并再次添加视图。优点是它在KITKAT之前的版本上也有相同的代码。
我做这种方式在我的代码:
ViewGroup parentView = (ViewGroup) (listView.getParent());
parentView.removeView(addFab);
parentView.addView(addFab);
这个工作对我来说:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="220dp"
android:gravity="bottom"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/details_wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="16dip"
android:scaleType="centerCrop"
android:src="@drawable/temp_nav_image">
</ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Kapil Rajput"
android:textStyle="bold"
android:textColor="@android:color/black"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textStyle="bold"
android:text="[email protected]"/>
</LinearLayout>
</RelativeLayout>
我用相对布局和意见在某种程度上决定的顺序哪一个是在顶部。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:splitTrack="false"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
在这种布局中,TextView的将是搜索条
制作父视图相对布局的顶部和xml.It添加您同时查看线性布局不会工作。 然后以编程方式添加FirstView.bringToFront()。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg"
android:orientation="vertical"
android:padding="15dp">
<ImageView
android:id="@+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginBottom="-30dp"
android:layout_marginTop="30dp"
android:src="@mipmap/login_logo" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/imgIcon"
android:layout_gravity="bottom"
android:background="#B3FFFFFF"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<View 1/>
<View 2/>
</LinearLayout>
Java file
imgIcon.brigtToFront();
- 1. Android:将视图放在与其他视图相同的高度
- 2. 以编程方式在其他视图上绘制ProgressBar
- 3. 以编程方式将视图添加到相对布局Android
- 4. 如何以编程方式在Android中隐藏布局/视图
- 5. 以编程方式在其他视图的底部添加子视图
- 6. SlidingDrawer上方其他视图
- 7. 如何在android中的其他视图添加覆盖视图?
- 8. 以编程方式动态文本视图的文本更改布局中的其他视图,我不想它
- 9. 将相对于其他视图在视图阵营本地
- 10. 如何在其他布局中充气视图?
- 11. LinearLayout在其他视图上方添加视图Android
- 12. 如何以编程方式在相对布局中添加视图
- 13. 在PreferenceActivity中显示其他视图(Android)
- 14. Android:以编程方式在布局视图上绘制形状
- 15. 相对视图内以编程方式编排布局下面
- 16. 的Android onTouch其他视图
- 17. Android:如何以编程方式在视图中添加视图?
- 18. 将列表视图与布局的其他元素对齐
- 19. Android - 约束布局 - 如何对齐视图中心在其他视图的边缘?
- 20. 检查其他视图中的视图?
- 21. 在android xml的其他视图的中间设置视图
- 22. 如何在android中的其他视图(自定义视图)上显示adview
- 23. iOS自动布局视图基于其他视图的高度
- 24. 视图变得隐藏在Android活动中的其他视图
- 25. 在Android中的其他视图下更新视图位置
- 26. 如何在iOS中的其他视图背后添加视图
- 27. 以编程方式在布局中布置子视图
- 28. 自动布局和在两个其他视图之间居中视图
- 29. 相对布局放置到其他相对布局
- 30. 如何在其他布局之间放置布局在RelativeLayout中?
请postyour代码 –