我在线性布局中有四个元素。
我想以这样的方式对齐它们,即 classALabel
和 classBLabel
并排对齐,占据屏幕的一半宽度。
并且 classA
和 classB
也并排对齐,占屏幕宽度的一半,但在 classALabel
下方 & classBLabel
.
以下是我目前已经尝试过的方法,但这并没有达到我想要的效果。它只是并排显示所有四个元素,每个元素占屏幕空间的 25%。
<LinearLayout
android:id="@+id/ClassALayout"
android:layout_width="match_parent"
android:layout_height="@dimen/tile_height"
android:layout_below="@+id/ClassLayout"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:id="@+id/classALabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/classALabel" />
<TextView
android:id="@+id/classA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/classALabel"
android:layout_marginTop="@dimen/dimen_2"
android:layout_alignParentLeft="true"
android:layout_weight="1"
/>
<TextView
android:id="@+id/classBLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/classA"
android:layout_weight="1"
android:text="@string/classBLabel" />
<TextView
android:id="@+id/classB"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/classBLabel"
android:layout_marginTop="@dimen/dimen_2"
android:layout_weight="1"
/>
</LinearLayout>
最佳答案
发生这种情况是因为您已将所有 View 放入水平方向的单个线性布局中。由于有 4 个 weight=1 的 View ,屏幕被分成 4 个相等的部分。
下面的代码应该可以帮助您实现您想要的。
ClassALayout 线性布局是父布局,它进一步包含两个子线性布局 - LabelContainer 线性布局和 ClassContainer 线性布局。 ClassContainer 布局位于 LabelContainer 布局下方,因为父布局具有垂直方向。两个子布局都包含 2 个 TextView 。 标签 TextView 在类 TextView 之上,它们都占据了 50% 的屏幕空间。
<LinearLayout
android:id="@+id/ClassALayout"
android:layout_width="match_parent"
android:layout_height="@dimen/tile_height"
android:layout_below="@+id/ClassLayout"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/LabelContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/classALabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/classALabel" />
<TextView
android:id="@+id/classBLabel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/classBLabel" />
</LinearLayout>
<LinearLayout
android:id="@+id/ClassContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/classA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_2"
android:layout_weight="1"/>
<TextView
android:id="@+id/classB"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dimen_2"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
关于android - 对齐 LinearLayout 中的元素占用一半的屏幕空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36433265/