如何在Android应用程序中对齐线性布局中的按钮?

如何在Android应用程序中对齐线性布局中的按钮?,android,android-xml,Android,Android Xml,我是一个初学者,正在尝试创建一个计算器 我无法在单个线性布局中对齐3个按钮 我的LinearLayout的calculator.xml代码是 <Button android:id="@+id/btnOne" android:layout_height="wrap_content" android:layout_weight="0.32" android:layout_width="107dp" andr

我是一个初学者,正在尝试创建一个计算器

我无法在单个
线性布局中对齐3个按钮

我的LinearLayout的calculator.xml代码是

    <Button
        android:id="@+id/btnOne"
        android:layout_height="wrap_content"
        android:layout_weight="0.32"
        android:layout_width="107dp"
        android:background="@android:color/darker_gray"
        android:text="1" />

    <Button
        android:id="@+id/btnPlus"
        android:layout_height="wrap_content"
        android:layout_weight="0.24"
        android:layout_width="107dp"
        android:text="+" />

    <Button
        android:id="@+id/btnEquals"
        android:layout_width="107dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.31"
        android:text="=" />
</LinearLayout>



如何将3个按钮放在一行中?

在包含
按钮的
线性布局中使用
android:orientation=“horizontal”

比如:

<LinearLayout
    android:orientation="vertical"     
    ..>

    <EditText>

    <LinearLayout
       android:orientation="horizontal" 
       ..>

          <Button .... />
          <Button .... />
          <Button .... />

    </LinearLayout>

</LinearLayout>

您可以在内部
线性布局上使用
android:orientation=“horizontal”
,正如其他用户所说:

<LinearLayout 
    android:orientation="horizontal"
    ......>
<Button
    DockPanel.
    android:id="@+id/btnOne"
    android:layout_height="wrap_content"
    android:layout_weight="0.32"
    android:layout_width="107dp"
    android:background="@android:color/darker_gray"
    android:text="1" />

<Button
    android:id="@+id/btnPlus"
    android:layout_height="wrap_content"
    android:layout_weight="0.24"
    android:layout_width="107dp"
    android:text="+" />

<Button
    android:id="@+id/btnEquals"
    android:layout_width="107dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.31"
    android:text="=" />
</LinearLayout>
我现在不尝试代码,所以请告诉我这是否是一个好的解决方案

您还可以使用
DockPanel
,等等。我建议你阅读任何关于android中
布局的好教程。这将对你理解布局有很大帮助


在第一个线性布局内使用第二个线性布局。将其放在编辑文本下。将它的方向设置为水平,并将所有按钮放在其中。设置按钮以填充父对象。将每个按钮的权重更改为1。现在,所有三个按钮的大小都相同。

使用内部线性布局来完成此操作:

<LinearLayout
           android:orientation="horizontal" 
            ......>

          <Button .... />
          <Button .... />
          <Button .... />

</LinearLayout>

<LinearLayout
           android:orientation="horizontal" 
            ......>

          <Button .... />
          <Button .... />
          <Button .... />

</LinearLayout>