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

Android 如何在LinearLayout中水平对齐3个固定宽度按钮?,android,android-linearlayout,Android,Android Linearlayout,我有这样的布局: <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingTop="32dp"> <Button android:layout_width="10

我有这样的布局:

<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个固定宽度按钮。你能帮我吗?

试试这个代码

<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>

基本上有三个步骤

  • 将weightSum=“3”设置为父布局。这意味着整个布局_权重之和为3

  • 将layout_weight=“1”设置为每个按钮。因此,每个按钮的大小都是父按钮的1/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>
    

    
    

  • 试试这个,根据你的要求改变重量

    <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>
    
    
    
    您可以将android:layout\u weight=“1”分配给每个按钮,但我希望保留我的原始宽度(100dp)。这就是为什么我发布这个问题。如果你想设置100dp。那你为什么要修改你写的代码呢?。