在android中的另一个视图上方放置/重叠(z-index)视图

新手上路,请多包涵

我有一个由 imageview 和 textview 组成的线性布局,在线性布局中一个在另一个之下。

 <LinearLayout android:orientation="horizontal" ... >
 <ImageView
     android:id="@+id/thumbnail"
     android:layout_weight="0.8"
     android:layout_width="0dip"
     android:layout_height="fill_parent">
 </ImageView>
 <TextView
    android:id="@+id/description"
    android:layout_weight="0.2"
    android:layout_width="0dip"
    android:layout_height="wrap_content">
 </TextView>

可能缺少一些规则,这是为了给出一个想法,布局的外观。我想要另一个长度和宽度为 50dip 的小文本视图,放置在 imageview 上,“over”是指 z-index 比 imageview 更多,我想将它放置在 imageview 的中心和上方(重叠)。

我想知道我们如何将一个视图放在另一个视图之上,并具有不同的 z-index(最好是线性布局)?

原文由 sat 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.6k
2 个回答

您不能为此使用 LinearLayout,但可以使用 FrameLayout 。在 FrameLayout 中,z-index 由添加项目的顺序定义,例如:

 <FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/my_drawable"
        android:scaleType="fitCenter"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:padding="5dp"
        android:text="My Label"
        />
</FrameLayout>

在这种情况下,TextView 将绘制在 ImageView 的顶部,沿着图像的底部中心。

原文由 Kevin Coppock 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您只想在需要时将一个视图置于前面,我会使用它:

 containerView.bringChildToFront(topView);

containerView 是要排序的视图的容器, topView 是我希望在容器中最顶部的视图。

对于要安排的多个视图,将使用 setChildrenDrawingOrderEnabled(true) 并覆盖 getChildDrawingOrder(int childCount, int i) ,如上所述。

原文由 Milan Jurkulak 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题