如何在LinearLayout中水平对齐3个固定宽度按钮?

时间:2015-06-19 06:43:30

标签: android android-linearlayout

我有这个布局:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="32dp">

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Button1"/>

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Button2"/>

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Button3"/>

    </LinearLayout>

我想水平对齐这3个修正宽度按钮。你能帮帮我吗?

3 个答案:

答案 0 :(得分:2)

试试此代码

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="32dp">

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button1"/>

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Button2"/>

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Button3"/>

</RelativeLayout>

答案 1 :(得分:1)

基本上涉及3个步骤。

  1. 将weightSum =“3”设置为父布局。这意味着整个layout_weights的总和为3。

  2. 将layout_weight =“1”设置为每个按钮。因此每个按钮的大小都是父级的1/3。

  3. 最后设置layout_width =“0dp”,这很重要,因为在这里你不必设置视图的宽度。它将由布局处理程序自动设置。

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button1"
            android:layout_weight="1"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button2"
            android:layout_weight="1"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Button3"
            android:layout_weight="1"/>
    
    </LinearLayout>
    

答案 2 :(得分:0)

尝试这个并根据您的要求改变体重

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="9" 
        android:orientation="horizontal"
        android:paddingTop="32dp">
        <Button
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:text="Button1"/>
        <Button
            android:layout_width="0dp"
             android:layout_weight="3"
            android:layout_height="wrap_content"
            android:text="Button2"/>
        <Button
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:text="Button3"/>
    </LinearLayout>