Reputation: 41076
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) ?
Upvotes: 266
Views: 353655
Reputation: 412
Try this in a RelativeLayout:
example XML:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/todo"
app:srcCompat="@drawable/ic_offer_close_outline"
app:tint="@color/colorWhite"
android:translationZ="999dp" />
example java:
ImageView image = new ImageView(this);
image.SetZ(float z);
Upvotes: 1
Reputation: 625
I use this, if you want only one view to be bring to front when needed:
containerView.bringChildToFront(topView);
containerView is container of views to be sorted, topView is view which i want to have as top most in container.
for multiple views to arrange is about to use setChildrenDrawingOrderEnabled(true) and overriding getChildDrawingOrder(int childCount, int i) as mentioned above.
Upvotes: 2
Reputation: 408
If you are adding the View programmatically, you can use yourLayout.addView(view, 1);
where 1 is the index
.
Upvotes: 1
Reputation: 643
I solved the same problem by add android:elevation="1dp"
to which view you want it over another. But it can't display below 5.0, and it will have a little shadow, if you can accept it, it's OK.
So, the most correct solution which is @kcoppock said.
Upvotes: 29
Reputation: 134664
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.
Upvotes: 320
Reputation: 81
Try this in a RelativeLayout:
ImageView image = new ImageView(this);
image.SetZ(float z);
It works for me.
Upvotes: 8
Reputation: 12935
Give a try to .bringToFront()
:
http://developer.android.com/reference/android/view/View.html#bringToFront%28%29
Upvotes: 202
Reputation: 18381
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 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];
}
}
Calling OrderLayout#setDrawOrder(int)
with 0-1-2 results in:
Upvotes: 31
Reputation: 8284
You can use view.setZ(float)
starting from API level 21. Here you can find more info.
Upvotes: 27
Reputation: 741
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>
Upvotes: 6
Reputation: 2721
RelativeLayout works the same way, the last image in the relative layout wins.
Upvotes: 72
Reputation: 28541
AFAIK you cannot do it with linear layouts, you'll have to go for a RelativeLayout.
Upvotes: 4