如何在Android应用程序中的LinearLayout中对齐按钮?

问题描述 投票:0回答:4

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

我无法在单个LinearLayout中对齐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>

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9zY01Oci5wbmcifQ==” alt =“在此处输入图像描述”>

我如何将三个按钮排成一行?

android android-xml
4个回答
0
投票

在第一个中使用第二个LinearLayout。将其放在edittext下。将其方向设置为水平,然后将所有按钮置于其中。设置按钮以填充父级。将每个按钮的权重更改为1。现在所有三个按钮的大小都相同。


0
投票

使用内部线性布局执行此操作:

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

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

</LinearLayout>

0
投票

您可以像其他用户所说的那样在内部android:orientation="horizontal"上使用LinearLayout

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

或者您可以尝试将按钮以StackPanel方向插入Horizontal内:

<StackPanel 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="=" />
</StackPanel>

我现在不尝试编码,所以请告诉您这是否是一个好的解决方案。

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

Any layout tutorial (random)


0
投票

使用包含android:orientation="horizontal"LinearLayout中的Button。类似于:

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

    <EditText>

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

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

    </LinearLayout>

</LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.