【问题标题】:Create three equally spaced (sub-) Layouts where 1st is on top and 3rd is on bottom创建三个等距(子)布局,其中第一个在顶部,第三个在底部
【发布时间】:2015-10-29 21:21:22
【问题描述】:

我已经尝试了很长时间,但没有为以下布局问题找到令人信服的解决方案。例如。如果我使用 RelativeLayout 作为容器,我可以使用(子)布局 A

 android:layout_alignParentTop="true"

对于(子)布局C

android:layout_alignParentBottom="true"> 

但是似乎不可能将布局 B 放置为两个空间具有相同高度的方式。有人在 .xml 中看到解决方案吗?

【问题讨论】:

  • 给 a & b layout id's and use (android:layout_below="@+id/the_id") for b & c。看看有没有帮助

标签: android android-layout space


【解决方案1】:

当将元素堆叠在一起时,LinearLayout 是一个简单的选择。由于子布局 A 和子布局 C 仅占用它们需要的空间,剩余空间随后分配给子布局 B 以及填充剩余空间所需的任何间距。这可以通过将子布局 B 包装在 FrameLayout 中来完成:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <FrameLayout
        android:id="@+id/sub_layout_a"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <FrameLayout
            android:id="@+id/sub_layout_b"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical">
        </FrameLayout>
    </FrameLayout>
    <FrameLayout
        android:id="@+id/sub_layout_c"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>
</LinearLayout>

您会注意到 sub_layout_asub_layout_bFrameLayout 可以是任何布局 - 只需确保 layout_height="wrap_content"(或固定值),以便它们只占用所需的空间。

通过使用layout_height="0dp"layout_weight="1" 将剩余空间分配给中间FrameLayout - layout_weight 将其拉伸以填充所有剩余空间。然后,我们可以使用android:layout_gravity="center_vertical" 将内部sub_layout_b 居中,在sub_layout_b 的两侧留下等量的空白空间。

【讨论】:

    【解决方案2】:

    试试这个(抱歉我现在不能测试):

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Any_layout_type_you_want
            android:id="@+id/sub_layout_a"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            ...
        </Any_layout_type_you_want>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
        <Any_layout_type_you_want
            android:id="@+id/sub_layout_b"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            ...
        </Any_layout_type_you_want>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
        <Any_layout_type_you_want
            android:id="@+id/sub_layout_c"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            ...
        </Any_layout_type_you_want>
    
    </LinearLayout>
    

    【讨论】:

    • 感谢 Fer,实际上这与来自 ianhanniballake 的原理相同,只是使用两个相同权重的占位符而不是一个垂直居中的占位符。该解决方案也应该可以完成这项工作。
    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多