【问题标题】:Android multiple buttons alignmentAndroid多按钮对齐
【发布时间】:2014-02-27 09:32:20
【问题描述】:

我正在测试Android Studio,我正在尝试编写一个简单的计算器来测试它并学习Android

我想要实现的是这样的:

我希望在每台设备上都有这种行为,无论是纵向还是横向。

我所做的是使所有内容都与字符串“Result is 0:”相关,所以是这样的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="org.mennini.mycalc.MainActivity$PlaceholderFragment">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Sum is: 0"
    android:id="@+id/textView"
    android:onClick="onButtonClick"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="8"
    android:id="@+id/button"
    android:layout_marginTop="40dp"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="7"
    android:id="@+id/button2"
    android:layout_alignTop="@+id/button"
    android:layout_toLeftOf="@+id/button"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="9"
    android:id="@+id/button3"
    android:layout_alignTop="@+id/button"
    android:layout_toRightOf="@+id/button"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="5"
    android:id="@+id/button4"
    android:layout_below="@+id/button"
    android:layout_toRightOf="@+id/button2"
    android:layout_marginTop="19dp"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="4"
    android:id="@+id/button5"
    android:layout_alignTop="@+id/button4"
    android:layout_toLeftOf="@+id/button4"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="6"
    android:id="@+id/button6"
    android:layout_alignTop="@+id/button4"
    android:layout_toRightOf="@+id/button4"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="2"
    android:id="@+id/button7"
    android:layout_below="@+id/button4"
    android:layout_toRightOf="@+id/button5"
    android:layout_marginTop="19dp"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1"
    android:id="@+id/button8"
    android:layout_alignTop="@+id/button7"
    android:layout_toLeftOf="@+id/button7"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="3"
    android:id="@+id/button9"
    android:layout_alignTop="@+id/button7"
    android:layout_toRightOf="@+id/button7"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:id="@+id/button10"
    android:layout_below="@+id/button7"
    android:layout_alignLeft="@+id/button8"
    android:layout_alignStart="@+id/button8"
    android:layout_marginTop="19dp"
    android:layout_toLeftOf="@+id/button9"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="."
    android:id="@+id/button11"
    android:layout_alignTop="@+id/button10"
    android:layout_toRightOf="@+id/button10"
    android:onClick="onButtonClick" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="C"
    android:id="@+id/button12"
    android:layout_below="@+id/button10"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:onClick="onButtonClick" />

列数少很容易,但如何处理偶数,例如示例中的?

更多,在 Android Studio 中,我尝试使用表格布局和网格布局,但我不明白如何设置具有特定行数和列数的网格,即 5 行 x 4 列网格,即我可以拖动一个 GridLayout 并设置行数和列数,但是当我在它上面拖动一些控件时,比如一个按钮,我看到一个绿色网格下面有比预期更多的单元格,所以我打算在我的 5x4 网格上绘制按钮并且让网格本身水平居中会令人沮丧。

最后(感谢您阅读此处)问题:我该如何处理这种情况?我使用与 textview 相关的所有按钮的想法是好是坏? 我使用 GridLayout 的想法是好主意还是愚蠢的主意?

我可以用什么聪明的方法来处理这个问题????

非常感谢

【问题讨论】:

标签: android android-layout button alignment android-studio


【解决方案1】:

你有没有想过使用TableLayout

【讨论】:

【解决方案2】:

您可以考虑前三列的垂直方向的线性布局(每行添加到具有水平方向的单独线性布局,这意味着您需要四个线性布局,然后全部添加到具有垂直方向的线性布局的第一个布局),然后将第一个线性布局和最后一列(首先添加到另一个垂直布局)添加到另一个水平方向的线性布局!

你也可以在这里找到很好的例子: http://mrbool.com/how-to-create-a-calculator-app-for-android/28100

这里还有完整的 xml 代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

         <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/plus"
            android:text="+"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/min"
            android:text="-"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/mul"
            android:text="*"
        />

      </LinearLayout>




         <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/seven"
            android:text="7"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/eight"
            android:text="8"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/nine"
            android:text="9"
        />

      </LinearLayout>

          <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/four"
            android:text="4"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/five"
            android:text="5"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/six"
            android:text="6"
        />

      </LinearLayout>

        <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/one"
            android:text="1"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/two"
            android:text="2"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/three"
            android:text="3"
        />

      </LinearLayout>


        <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/one"
            android:text="1"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/two"
            android:text="2"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/three"
            android:text="3"
        />

      </LinearLayout>

        <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/zero"
            android:layout_weight="2"
            android:text="0"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/dot"
            android:text="."
        />

      </LinearLayout>

   </LinearLayout>
     <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/slash"

            android:text="/"
        />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id = "@+id/enter"
            android:layout_weight="2"
            android:text="Enter"
        />

      </LinearLayout>


</LinearLayout>

问候@Sandman42,

【讨论】:

    【解决方案3】:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".Home" android:background="#fff" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/disp" android:id = "@+id/display" android:hint="@string/dispHint" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="20dp"> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/seven" android:text="@string/seven" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/eight" android:text="@string/eight" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/nine" android:text="@string/nine" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/div" android:text="@string/div" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="20dp"> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/four" android:text="@string/four" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/five" android:text="@string/five" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/six" android:text="@string/six" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/mul" android:text="@string/mul" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="20dp"> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/one" android:text="@string/one" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/two" android:text="@string/two" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/three" android:text="@string/three" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/sub" android:text="@string/sub" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingTop="20dp"> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/cancel" android:text="@string/cancel" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/zero" android:text="@string/zero" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/equal" android:text="@string/equal" /> <Button android:layout_width="55dp" android:layout_height="wrap_content" android:id = "@+id/add" android:text="@string/add" /> </LinearLayout> </LinearLayout>
    

    【讨论】:

    • 添加一些解释并格式化代码。
    【解决方案4】:

    您可以使用 linearLayout 并赋予所需的权重。 这是我的代码,我为每行赋予 1/5=0.2 权重,为每列赋予 1/4=0.25。

    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    >
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="onButtonClick"
        android:text="Sum is: 0"
        android:textAppearance="?android:attr/textAppearanceLarge"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="32dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:orientation="horizontal">
    
                <Button
                    android:id="@+id/button14"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:text="+" />
    
                <Button
                    android:id="@+id/button15"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:text="-" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:orientation="horizontal">
    
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:text="7" />
    
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:text="8" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:orientation="horizontal">
    
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:text="4" />
    
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:text="5" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:orientation="horizontal">
    
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:text="1" />
    
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.25"
                    android:text="2" />
            </LinearLayout>
    
            <Button
                android:id="@+id/button17"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:text="0" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:orientation="vertical">
    
            <Button
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:text="*" />
    
            <Button
                android:id="@+id/button7"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:text="9" />
    
            <Button
                android:id="@+id/button8"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:text="6" />
    
            <Button
                android:id="@+id/button9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:text="3" />
    
            <Button
                android:id="@+id/button10"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:text="." />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25"
            android:orientation="vertical">
    
            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.2"
                android:text="/" />
    
            <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.8"
                android:text="Enter"
                android:textSize="12sp"/>
        </LinearLayout>
    </LinearLayout>
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-06
      • 2011-05-12
      • 2011-06-22
      • 2012-02-11
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多