Placing/Overlapping(z-index) a view above another

2019-01-01 00:20发布

问题:

I have a linear layout which consists of imageview and textview , one below another in a linear layout.

<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>

Some rules might be missing , this is to give an idea , how layout looks. I want another small text view of say 50dip in length and width , placed over the imageview, by \"over\" I meant z-index more than imageview , I want to place this , in the center and above(overlapping) the imageview.

I want to know how can we place one view above the other, with varying z-index (preferably in linear layout) ?

回答1:

You can\'t use a LinearLayout for this, but you can use a FrameLayout. In a FrameLayout, the z-index is defined by the order in which the items are added, for example:

<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>

In this instance, the TextView would be drawn on top of the ImageView, along the bottom center of the image.



回答2:

Give a try to .bringToFront():

http://developer.android.com/reference/android/view/View.html#bringToFront%28%29



回答3:

RelativeLayout works the same way, the last image in the relative layout wins.



回答4:

Changing the draw order

An alternative is to change the order in which the views are drawn by the parent. You can enable this feature from ViewGroup by calling setChildrenDrawingOrderEnabled(true) and overriding getChildDrawingOrder(int childCount, int i).

Example:

/**
 * 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];
    }
}

Output:

Calling OrderLayout#setDrawOrder(int) with 0-1-2 results in:

\"enter \"enter \"enter



回答5:

You can use view.setZ(float) starting from API level 21. Here you can find more info.



回答6:

There is a way to use LinearLayout. Just set the marginTop of your previous element to the corresponding negative value, and make sure the element you want on top is after the element you want below in your 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>


回答7:

Try this in a RelativeLayout:

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

It works for me.



回答8:

AFAIK you cannot do it with linear layouts, you\'ll have to go for a RelativeLayout.



回答9:

I solved the same problem by add android:elevation=\"1dp\" to which view you want it over another.