0

我正在尝试为我的应用程序编写一个替代的横向视图文件(一个 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>  
4

1 回答 1

2

您应该使用相对布局或线性布局来实现这一点,因为框架布局根本不是为此而设计的。这是框架布局的api文档-

FrameLayout 旨在阻止屏幕上的一个区域以显示单个项目。通常,应该使用 FrameLayout 来保存单个子视图,因为很难以一种可缩放到不同屏幕尺寸的方式来组织子视图,而不会使子视图相互重叠。但是,您可以使用 android:layout_gravity 属性将多个子项添加到 FrameLayout 并通过为每个子项分配重力来控制它们在 FrameLayout 中的位置。

所以在 Frame 布局中控制 child 的位置是非常有限的,即仅使用重力。

于 2014-01-01T09:01:00.147 回答