我有一个线性布局,其中包括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索引(最好是线性布局)?


当前回答

我使用这个,如果你只希望在需要时将一个视图带到前面:

containerView.bringChildToFront(topView);

containerView是容器的视图要排序,topView是视图,我想有作为最顶端的容器。

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

其他回答

有一种使用线性布局的方法。只需将前一个元素的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>

在RelativeLayout中试试这个:

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

这对我很管用。

我使用这个,如果你只希望在需要时将一个视图带到前面:

containerView.bringChildToFront(topView);

containerView是容器的视图要排序,topView是视图,我想有作为最顶端的容器。

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

使用android: transformZ。它也适用于线性布局。

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