【问题标题】:Have elements equally span entire width / height of their RelativeLayout让元素均匀地跨越其 RelativeLayout 的整个宽度/高度
【发布时间】:2015-02-03 12:39:33
【问题描述】:

我有,比如说三个按钮,我想在布局中一个一个地在下面列出。根据设备、屏幕尺寸、像素密度、方向等,我希望按钮跨越其父布局的整个高度。每个按钮都有一个固定的高度 (dp),因此跨度更可能与按钮之间的空间有关。

我在各种论坛上看到了几个关于 LinearLayout 如何解决这个问题的问题,方法是为每个元素嵌套一个布局,具有布局跨度。我非常想避免嵌套布局,而且我现在使用的是 RelativeLayout,所以如果有任何方法可以使用这种类型的布局,那将会有很大的帮助! :)

另外,我希望顶部和底部按钮“触摸”顶部和底部的父布局边框,最后一个(或休息)按钮平等地填充垂直空间的其余部分。

提前谢谢你。

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    我不完全确定您希望实现什么,但您应该可以使用 LinearLayoutweights 来做到这一点(因此您不必嵌套多个布局)。

    如果您希望 3 个按钮占据整个父屏幕,只需为每个按钮添加一个 weight,例如:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:id="@+id/button_one"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    
    <Button
        android:id="@+id/button_two"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    
    <Button
        android:id="@+id/button_three"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    
    </LinearLayout>
    

    【讨论】:

    • 是的,但是这会将按钮集中在垂直中间,我希望顶部和底部按钮触摸父布局边框,最后一个(或其余)填充其余部分。
    • 另外,这会拉伸按钮以填充垂直空间。我正在尝试拉伸按钮之间的空间。 :)
    【解决方案2】:
    • 尝试将topButton 中的字段设置为XML: android:layout_alignTop="true";
    • 尝试将middleButton 中的字段设置为XML: android:layout_centerVertical="true";
    • 尝试将XML: android:layout_alignBottom="true";中的一个字段设置为bottomButton

    【讨论】:

    • android:align_parent 不存在?这对于三个按钮可能是正确的。如果我想要四个或五个按钮,这种方法不会很好用吗? ://
    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    相关资源
    最近更新 更多