我将四个 ImageView 放置在垂直线性布局上。我希望它们占用相同的空间,所以我为每个分配了一个 android:layout_weight="1"
。我还希望它们重叠(这是设计要求),所以我为每个 View 设置了负边距。我添加的最后一张图片 (@+id/floor_1st
) 是最后添加的(底部的一张),因此它位于最前面。但是,我希望它是另一种方式:我希望布局上的第一张图片位于前面,然后是第二张,依此类推(最后一张图片应位于后面)。
我知道使用 RelativeLayout
可以更轻松地控制图像的放置顺序,但我不知道如何使用此布局以我想要的方式放置图像。我还看到可以使用 bringToFront()
方法,但只是不要让图像重叠。
那么,有什么方法可以使用 LinearLayout
以我想要的顺序放置图像?还是我应该使用其他布局?在这种情况下,我应该如何放置图像?
这是我的xml代码
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/floors_container"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/overview_buttons_top_margin"
android:layout_marginBottom="@dimen/overview_buttons_bottom_margin"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/floor_4th"
android:src="@drawable/piso_4"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="@dimen/floors_overview_margin_three_quarter"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/floor_3rd"
android:src="@drawable/piso_3"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:layout_marginTop="@dimen/floors_overview_margin_quarter"
android:layout_marginBottom="@dimen/floors_overview_margin_half"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/floor_2nd"
android:layout_gravity="center_horizontal"
android:src="@drawable/piso_2"
android:layout_weight="1"
android:layout_marginTop="@dimen/floors_overview_margin_half"
android:layout_marginBottom="@dimen/floors_overview_margin_quarter"
android:clickable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/floor_1st"
android:src="@drawable/piso_1"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:layout_marginTop="@dimen/floors_overview_margin_three_quarter"
android:clickable="true" />
</LinearLayout>
谢谢。
最佳答案
如果你想反转绘制顺序,你需要继承 LinearLayout 类并重写 getChildDrawingOrder。
@Override
protected int getChildDrawingOrder(int childCount, int i) {
//The standard implementation just retuns i
return childCount - i - 1;
}
确保在某处启用自定义排序:
setChildrenDrawingOrderEnabled(true);
关于android - 在 android 上的 LinearLayout 上设置 "z-index",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30091466/