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

时间:2010-11-15 08:03:43

标签: android android-layout 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上,&#34; over&#34;我的意思是z-index比imageview更多,我想把它放在图像视图的中心和上方(重叠)。

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

11 个答案:

答案 0 :(得分:273)

您无法使用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的顶部沿着图像的底部中心绘制。

答案 1 :(得分:170)

答案 2 :(得分:60)

RelativeLayout以相同的方式工作,相对布局中的最后一个图像获胜。

答案 3 :(得分:22)

更改绘制顺序

另一种方法是更改​​父级绘制视图的顺序。您可以通过调用setChildrenDrawingOrderEnabled(true)并覆盖getChildDrawingOrder(int childCount, int i)来启用ViewGroup中的此功能。

实施例

/**
 * Example Layout that changes draw order of a FrameLayout
 */
public class OrderLayout extends FrameLayout {

    private static final int[][] DRAW_ORDERS = new int[][]{
            {0, 1, 2},
            {2, 1, 0},
            {1, 2, 0}
    };

    private int currentOrder;

    public OrderLayout(Context context) {
        super(context);
        setChildrenDrawingOrderEnabled(true);
    }

    public void setDrawOrder(int order) {
        currentOrder = order;
        invalidate();
    }

    @Override
    protected int getChildDrawingOrder(int childCount, int i) {
        return DRAW_ORDERS[currentOrder][i];
    }
}

输出:

使用0-1-2调用OrderLayout#setDrawOrder(int)会导致:

enter image description here enter image description here enter image description here

答案 4 :(得分:16)

您可以从API级别21开始使用view.setZ(float)Here您可以找到更多信息。

答案 5 :(得分:9)

我通过将android:elevation="1dp"添加到您想要的视图来解决同一问题。但它不能显示在5.0以下,它会有一点阴影,如果你能接受它,那就没关系。

所以,最正确的解决方案是@kcoppock说。

答案 6 :(得分:6)

在RelativeLayout中尝试:

ImageView image = new ImageView(this);
image.SetZ(float z);

它对我有用。

答案 7 :(得分:5)

有一种方法可以使用LinearLayout。只需将前一个元素的marginTop设置为相应的负值,并确保您想要的元素位于XML下面所需的元素之后。

<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_marginTop="-20dip"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="wrap_content"
>
</TextView>

答案 8 :(得分:4)

AFAIK你无法使用线性布局,你必须选择RelativeLayout

答案 9 :(得分:0)

如果要以编程方式添加视图,则可以使用yourLayout.addView(view, 1);

其中1 is the index

答案 10 :(得分:0)

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

containerView.bringChildToFront(topView);

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

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

相关问题