0

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

我无法对齐单个 3 按钮LinearLayout

我的 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 个按钮排成一行?

4

4 回答 4

0

android:orientation="horizontal"LinearLayout包含Buttons 的中使用。
就像是:

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

    <EditText>

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

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

    </LinearLayout>

</LinearLayout>
于 2013-04-08T15:59:52.547 回答
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, 左右。我建议您阅读有关Layoutsandroid 的任何好的教程。这将对您理解布局有很大帮助。

任何布局教程(随机)

于 2013-04-08T16:01:24.443 回答
0

在第一个内部使用第二个 LinearLayout。把它放在edittext下面。将其方向设置为水平并将所有按钮放入其中。将按钮设置为填充父级。将每个按钮的权重更改为 1。现在所有三个按钮的大小将相同。

于 2013-04-08T16:02:00.797 回答
0

使用内部线性布局来做到这一点:

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

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

</LinearLayout>
于 2013-04-08T16:10:39.963 回答