【问题标题】:How to layout 4 views side by side with 1 view centered如何以 1 个视图为中心并排布局 4 个视图
【发布时间】:2017-06-13 18:22:41
【问题描述】:

我正在尝试在我的活动顶部布局 4 个视图(2 个按钮,2 个 TextView)。我需要 button_2 水平居中,TextView_2 占据视图右侧的其余空间,button_1 到 wrap_content 并固定在父级的左侧,textView_1 填充 button_1 和 button_2 之间的空间。

到目前为止我的代码:

 <LinearLayout
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<Button
    android:id="@+id/button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<TextView
    android:id="@+id/textView_1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<Button
    android:id="@+id/button_2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_weight="1"/>
<TextView
    android:id="@+id/textView_2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>

【问题讨论】:

    标签: android xml


    【解决方案1】:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="button_1"/>
    <TextView
        android:id="@+id/textView_1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/button_1"
        android:layout_toLeftOf="@+id/button_2"
        android:layout_alignBottom="@+id/button_2"
        android:text="textView_1"/>
    <Button
        android:id="@+id/button_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="button_2"/>
    <TextView
        android:id="@+id/textView_2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="textView_2"
        android:gravity="center"
        android:layout_alignBottom="@+id/button_2"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/button_2" />
    
       </RelativeLayout>
    

    【讨论】:

    • 谢谢。我显然在尝试使用线性布局时走错了路!
    • 一条评论,相对布局高度需要改成wrap_content或者覆盖整个屏幕。
    【解决方案2】:

    Relative Layout 将完全适合您的情况。明智地使用android:layout_toRightOfandroid:layout_toLeftOfandroid:layout_alignParentRightandroid:layout_alignParentLeft 属性。

    <RelativeLayout
            android:layout_width="match_parent" android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/button_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"/>
            <TextView
                android:id="@+id/textView_1"
                android:layout_toRightOf="@+id/button_1"
                android:layout_toleftOf="@+id/button_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hello there"/>
            <Button
                android:id="@+id/button_2"
                android:layout_width="wrap_content"
                android:layout_toRightOf="@+id/textView_1"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"/>
            <TextView
                android:id="@+id/textView_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/button_2"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>
    

    【讨论】:

    • 谢谢,我相信这与 anmol 的答案相同。我在尝试使用线性布局时走错了路!
    • 是的,当您需要更复杂的对齐方式时,相对布局会更好。谢谢
    【解决方案3】:

    试试这个

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1">
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:layout_weight="1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1">
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>
    </LinearLayout>
    

    删除子线性布局中的布局权重,更改高度以包装 textview 和按钮的内容以满足您的要求

    【讨论】:

    • 建议的其他 2 个答案的相对布局似乎是解决问题的正确方法。感谢您的宝贵时间。
    • 很高兴
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2021-09-28
    • 2015-09-21
    • 2023-04-04
    • 1970-01-01
    • 2020-02-12
    • 2019-02-09
    • 1970-01-01
    相关资源
    最近更新 更多