我正在尝试为我的应用程序编写一个替代的横向视图文件(一个 xml 文件),我必须使用 FrameLayout 而不是 LinearLayout(这就是书中所说的)。但是 Framelayout 并没有很好的堆叠,所以我们应该使用android:layout_gravity
然后分配一个 x/y 维度,例如:android:layout_gravity="center"
. 这个例子正好在中间(垂直和水平)居中。
但我的问题是,我在垂直平面上有 4 个级别,我想在其中放置东西。一行文本,一行有 2 个按钮(真,假),另一个按钮(作弊),最后一行有 2 个箭头按钮(上一个和下一个)。但是使用 layout_gravity,它们只有非常粗略的位置:顶部、中心、底部。我注意到,如果您不分配任何内容,它们最终都会出现在左上角。
那么我如何垂直堆叠这些,以便它们从上到下很好地间隔开呢?为 2 个事物分配相同的参数不会将它们堆叠起来,而是将它们严重重叠。
感谢您的帮助,下面是我的代码。我还没有在其中放置任何重力布局。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/question_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button" />
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button" />
</LinearLayout>
<Button
android:id="@+id/cheat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cheat_button" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_left"
android:contentDescription="@string/move_back"
/>
<ImageButton
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_right"
android:contentDescription="@string/move_forward"
/>
</LinearLayout>
</FrameLayout>