【发布时间】:2015-12-11 02:21:36
【问题描述】:
在对线性布局以及如何使用线性布局完成定位任务进行大量搜索后,我发现了很多问题,流行的答案是“使用相对布局”。
我想深入了解线性布局,但很难找到一个解释来解决我认为应该很简单的任务。一个有意义的解决方案似乎也是合理的。
如果我将屏幕视为井字棋盘并想在每个位置放置一个按钮,我该如何在不依赖前一个按钮的宽度进行堆叠或边距的情况下做到这一点。
例如,如果我想跳过广场上的某个位置,我跳过的位置将保持空白。
{1} {2} {3}
{4} {5} {6}
{7} {8} {9}
根据我阅读的 XML,我的按钮应该位于 1 3 5 7 9 但这不是那个结果。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/myLayout" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/Button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:text="@string/btn1" />
<Button
android:id="@+id/Button2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:text="@string/btn2" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/Button3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/btn3" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/Button4"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:text="@string/btn4" />
<Button
android:id="@+id/Button5"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="@string/btn5" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/background"
android:background="@drawable/background"/>
</LinearLayout>
我的结果是,
{1} {2} { }
{ } {5} { }
{7} {8} { }
我差一点就成功了! (我认为...)
将 2 和 8 向右移动一个空格我缺少什么?我没有按照指示使用线性布局吗?
这是我的目标。
{1} { } {2}
{ } { } { }
{ } { } { }
{ } {3} { }
{ } { } { }
{ } { } { }
{4} { } {5}
【问题讨论】:
-
如果你有这么多水平和垂直视图,为什么不使用
RelativeLayout或FrameLayout而不是LinearLayout? -
因为我正在努力学习如何使用线性布局。
-
这种布局会对性能产生影响,因为计算每个布局的屏幕密度需要时间,而且数量很多。最好尝试不同的方法学习
LinearLayouts。 -
我不是在尝试构建企业应用程序。我试图了解线性布局如何深入工作。我不相信我在互联网上找到的关于线性布局的每个问题的解决方案都是“使用相对布局”。我只是想学习控制它们,所以当我制作企业应用程序并且需要线性布局时,我将能够正确使用它。
标签: android xml android-layout android-linearlayout