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) ?
This question is related to
android
android-layout
z-index
Give a try to .bringToFront()
:
http://developer.android.com/reference/android/view/View.html#bringToFront%28%29
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>
RelativeLayout works the same way, the last image in the relative layout wins.
AFAIK you cannot do it with linear layouts, you'll have to go for a RelativeLayout.
If you are adding the View programmatically, you can use yourLayout.addView(view, 1);
where 1 is the index
.
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.
You can use view.setZ(float)
starting from API level 21. Here you can find more info.
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:
Try this in a RelativeLayout:
ImageView image = new ImageView(this);
image.SetZ(float z);
It works for me.
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.
Source: Stackoverflow.com