Nic*_*ias 5 android android-layout
我在垂直线性布局上放置了四个图像视图.我希望他们能够在同一个空间中占据一席之地,所以我分配给每个人android:layout_weight="1".我也希望它们重叠(这是一个设计要求),所以我为每个视图设置了一个负余量.我添加的最后一个图像(@+id/floor_1st)是最后添加的图像(底部的图像),因此它保留在前面.但是,我希望它是另一种方式:我希望布局上的第一个图像位于前面,然后是第二个,依此类推(最后一个图像位于后面).
我知道使用a控制图像的顺序更容易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>
Run Code Online (Sandbox Code Playgroud)
谢谢.
如果要反转绘制顺序,则需要子类化LinearLayout类并覆盖getChildDrawingOrder.
@Override
protected int getChildDrawingOrder(int childCount, int i) {
//The standard implementation just retuns i
return childCount - i - 1;
}
Run Code Online (Sandbox Code Playgroud)
确保在某处启用自定义顺序:
setChildrenDrawingOrderEnabled(true);
Run Code Online (Sandbox Code Playgroud)
对于 Android 21 级以上,您可以使用 view.setZ() http://developer.android.com/reference/android/view/View.html#Drawing
对于低于 21 的 Android 级别,我建议使用 FrameLayout 或relativelayout 与 BringToFront() 和/或负填充、边距(如果需要)结合使用。对于bringToFront()方法的使用,请参考Defining Z order of views ofrelativeLayout in Android
| 归档时间: |
|
| 查看次数: |
19630 次 |
| 最近记录: |