sat*_*sat 220 android z-index android-layout
我有一个线性布局,包括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>
Run Code Online (Sandbox Code Playgroud)
可能缺少一些规则,这是为了给出一个想法,布局看起来如何.我希望另一个小文本视图说50dip的长度和宽度,放在imageview上,"over"我的意思是z-index比imageview更多,我想把它放在imageview的中心和上面(重叠).
我想知道如何将一个视图放在另一个视图上方,具有不同的z-index(最好是线性布局)?
kco*_*ock 286
你不能使用LinearLayout,但你可以使用FrameLayout
.在a中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>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,TextView将沿着图像的底部中心绘制在ImageView的顶部.
diy*_*ism 181
尝试.bringToFront()
:
http://developer.android.com/reference/android/view/View.html#bringToFront%28%29
jt-*_*son 64
RelativeLayout以相同的方式工作,相对布局中的最后一个图像获胜.
Tob*_*run 26
另一种方法是更改父级绘制视图的顺序.您可以通过调用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];
}
}
Run Code Online (Sandbox Code Playgroud)
OrderLayout#setDrawOrder(int)
使用0-1-2 调用会导致:
thi*_*uan 14
我通过添加android:elevation="1dp"
你想要的视图来解决同一个问题.但它不能显示在5.0以下,它会有一点阴影,如果你可以接受它,它没关系.
所以,最正确的解决方案是@kcoppock说.
小智 7
在RelativeLayout中尝试这个:
ImageView image = new ImageView(this);
image.SetZ(float z);
Run Code Online (Sandbox Code Playgroud)
这个对我有用.
有一种方法可以使用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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
283460 次 |
最近记录: |