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>
基本上有三个步骤
<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。那你为什么要修改你写的代码呢?。