布局管理的使用——计算器界面设计(LinearLayout,TableLayout,ConstraintLayout三种方式)

题目要求:

分别用布局管理器LinearLayout,TableLayout,ConstraintLayout设计计算器的界面如图所示,基本组件有TextView、Button。通过本次作业,熟悉每种布局管理的特点及适用场景,掌握layout_weight的方法和使用。
在这里插入图片描述

一. LinearLayout

首先,我们需要明白LinearLayout布局的意思。这是一个线性布局。属性有水平或者竖直。接下来,开始完成任务。
第一步,先建一个LinearLayout。在res/layout/下新建一个calumniate_LinearLayout.xml的布局文件。(操作步骤为右击res目录下的layout,选择new Resource File,这里要注意把Root element修改为LinearLayout)
第二步,先加入代码:android:orientation=“vertical”,设置我们的线性布局是垂直方向的。
这是线性布局必须要设置的!!!
第三步,先设置“0”的显示,加入一个TextView, android:layout_width=“match_parent”
android:layout_height=“0dp”
android:layout_weight=“0.229”
android:gravity=“right|bottom”
android:text=“0”
android:textSize=“65dp”
第四步,开始写下面的7行布局,因为结构都是一样的,这里我就写第一行,剩下的自行修改即可。具体为先嵌套一个LinearLayout,然后加入Button按钮,设置权重以及显示的内容,代码如下:
<按钮
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_marginLeft=“3dp”
android:layout_marginBottom=“1dp”
android:layout_weight=“0.167”
android:text=“MC” />
注:按钮就是Button,因为CSDN不好打出来。
第五步,按照上面的步骤继续写6个LinearLayout就好了。

全部代码如下:

// LinearLayout布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#C0C0C0">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.229"
        android:gravity="right|bottom"
        android:text="0"
        android:textSize="65dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.073">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="3dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.167"
            android:text="MC" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.167"
            android:text="MR" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.167"
            android:text="M+" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.167"
            android:text="M-" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.167"
            android:text="MS" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="3dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.167"
            android:text="M*" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.116">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="%" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="√" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="x²" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginRight="3dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="1/x" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.116">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="CE" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="C" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="×" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginRight="3dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="÷" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.116">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="7" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="8" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="9" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginRight="3dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="X" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.116">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="4" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="5" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="6" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginRight="3dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="-" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.116">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="1" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="2" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="3" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginRight="3dp"
            android:layout_marginBottom="1dp"
            android:layout_weight="0.25"
            android:text="+" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.116">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="3dp"
            android:layout_weight="0.25"
            android:text="±" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="3dp"
            android:layout_weight="0.25"
            android:text="0" />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="3dp"
            android:layout_weight="0.25"
            android:text="." />

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="1dp"
            android:layout_marginRight="3dp"
            android:layout_marginBottom="3dp"
            android:layout_weight="0.25"
            android:text="=" />
    </LinearLayout>
</LinearLayout>

二.TableLayout

这里也是同理,先设置TableLayout的xml文件,然后开始完成任务。
这里我们需要注意的是:一个TableRow代表一行。
其他的原理同LinearLayout。
全部代码如下:

// TableLayout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C0C0C0">

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="3.55">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="right|bottom"
            android:text="0"
            android:textSize="50dp" />
    </TableRow>

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="1">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="MC" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="MR" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="M+" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="M-" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="MS" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="M*" />
    </TableRow>

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="1.66">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="%" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="√" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="x²" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="1/x" />
    </TableRow>

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="1.66">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="CE" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="C" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="×" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="÷" />
    </TableRow>

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="1.66">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="7" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="8" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="9" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="X" />
    </TableRow>

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="1.66">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="4" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="5" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="6" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="-" />
    </TableRow>

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="1.66">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="1" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="2" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="3" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="+" />
    </TableRow>

    <TableRow
        android:layout_height="0dp"
        android:layout_weight="1.66">

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"

            android:text="±" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="0" />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="." />

        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="=" />
    </TableRow>

</TableLayout>

三.ConstraintLayout

这个约束布局就是比较简单了,可以直接在布局文件里面通过拉动组件来拉出来。
全部代码如下:

// ConstraintLayout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="136dp"
        android:text="MC"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="MR"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="M+"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toTopOf="@+id/button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="M-"
        app:layout_constraintEnd_toStartOf="@+id/button5"
        app:layout_constraintStart_toEndOf="@+id/button3"
        app:layout_constraintTop_toTopOf="@+id/button3" />

    <Button
        android:id="@+id/button5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="MS"
        app:layout_constraintEnd_toEndOf="@+id/button6"
        app:layout_constraintStart_toEndOf="@+id/button4"
        app:layout_constraintTop_toTopOf="@+id/button4" />

    <Button
        android:id="@+id/button6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="M*"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button5"
        app:layout_constraintTop_toTopOf="@+id/button5" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="%"
        app:layout_constraintBottom_toTopOf="@+id/button15"
        app:layout_constraintEnd_toStartOf="@+id/button8"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button"
        app:layout_constraintVertical_bias="0.77" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="√"
        app:layout_constraintEnd_toStartOf="@+id/button9"
        app:layout_constraintStart_toEndOf="@+id/button7"
        app:layout_constraintTop_toTopOf="@+id/button7" />

    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="x²"
        app:layout_constraintEnd_toStartOf="@+id/button10"
        app:layout_constraintStart_toEndOf="@+id/button8"
        app:layout_constraintTop_toTopOf="@+id/button8" />

    <Button
        android:id="@+id/button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1/x"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button9"
        app:layout_constraintTop_toTopOf="@+id/button9" />

    <Button
        android:id="@+id/button15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CE"
        app:layout_constraintBottom_toTopOf="@+id/button19"
        app:layout_constraintEnd_toStartOf="@+id/button16"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button7" />

    <Button
        android:id="@+id/button16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="C"
        app:layout_constraintEnd_toStartOf="@+id/button17"
        app:layout_constraintStart_toEndOf="@+id/button15"
        app:layout_constraintTop_toTopOf="@+id/button15" />

    <Button
        android:id="@+id/button17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="×"
        app:layout_constraintEnd_toStartOf="@+id/button18"
        app:layout_constraintStart_toEndOf="@+id/button16"
        app:layout_constraintTop_toTopOf="@+id/button16" />

    <Button
        android:id="@+id/button18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="÷"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button17"
        app:layout_constraintTop_toTopOf="@+id/button17" />

    <Button
        android:id="@+id/button19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7"
        app:layout_constraintBottom_toTopOf="@+id/button23"
        app:layout_constraintEnd_toStartOf="@+id/button20"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button15" />

    <Button
        android:id="@+id/button20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        app:layout_constraintEnd_toStartOf="@+id/button21"
        app:layout_constraintStart_toEndOf="@+id/button19"
        app:layout_constraintTop_toTopOf="@+id/button19" />

    <Button
        android:id="@+id/button21"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"
        app:layout_constraintEnd_toStartOf="@+id/button22"
        app:layout_constraintStart_toEndOf="@+id/button20"
        app:layout_constraintTop_toTopOf="@+id/button20" />

    <Button
        android:id="@+id/button22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="X"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button21"
        app:layout_constraintTop_toTopOf="@+id/button21" />

    <Button
        android:id="@+id/button23"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4"
        app:layout_constraintBottom_toTopOf="@+id/button32"
        app:layout_constraintEnd_toStartOf="@+id/button29"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button19" />

    <Button
        android:id="@+id/button29"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        app:layout_constraintEnd_toStartOf="@+id/button30"
        app:layout_constraintStart_toEndOf="@+id/button23"
        app:layout_constraintTop_toTopOf="@+id/button23" />

    <Button
        android:id="@+id/button31"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button30"
        app:layout_constraintTop_toTopOf="@+id/button30" />

    <Button
        android:id="@+id/button30"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        app:layout_constraintEnd_toStartOf="@+id/button31"
        app:layout_constraintStart_toEndOf="@+id/button29"
        app:layout_constraintTop_toTopOf="@+id/button29" />

    <Button
        android:id="@+id/button32"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        app:layout_constraintBottom_toTopOf="@+id/button36"
        app:layout_constraintEnd_toStartOf="@+id/button33"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button23" />

    <Button
        android:id="@+id/button33"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        app:layout_constraintEnd_toStartOf="@+id/button34"
        app:layout_constraintStart_toEndOf="@+id/button32"
        app:layout_constraintTop_toTopOf="@+id/button32" />

    <Button
        android:id="@+id/button34"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        app:layout_constraintEnd_toStartOf="@+id/button35"
        app:layout_constraintStart_toEndOf="@+id/button33"
        app:layout_constraintTop_toTopOf="@+id/button33" />

    <Button
        android:id="@+id/button35"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button34"
        app:layout_constraintTop_toTopOf="@+id/button34" />

    <Button
        android:id="@+id/button36"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="±"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button37"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button32" />

    <Button
        android:id="@+id/button37"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        app:layout_constraintEnd_toStartOf="@+id/button38"
        app:layout_constraintStart_toEndOf="@+id/button36"
        app:layout_constraintTop_toTopOf="@+id/button36" />

    <Button
        android:id="@+id/button38"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="."
        app:layout_constraintEnd_toStartOf="@+id/button39"
        app:layout_constraintStart_toEndOf="@+id/button37"
        app:layout_constraintTop_toTopOf="@+id/button37" />

    <Button
        android:id="@+id/button39"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="="
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button38"
        app:layout_constraintTop_toTopOf="@+id/button38" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="361dp"
        android:layout_height="134dp"
        android:gravity="right|bottom"
        android:text="0"
        android:textSize="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

说明

该文章仅供大家学习交流使用,如有哪里不妥,请大家批评指正。
对代码有哪里不理解的,可以留言评论,不定时回复!

  • 3
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android Studio 计算器布局可以使用 LinearLayout 或 GridLayout 进行设计。其中,LinearLayout 是一种线性布局,可以按照水平或垂直方向排列视图,而 GridLayout 则是一种网格布局,可以将视图按照行列进行排列。 在设计计算器布局时,需要考虑到各个按钮的位置和大小,以及布局的整体风格和配色。可以使用 Android Studio 提供的布局编辑器进行设计,也可以手动编写 XML 布局文件。 在布局文件中,需要定义各个视图的属性,如宽度、高度、边距、背景颜色等。同时,还需要为按钮添加点击事件,以实现计算器的功能。 总之,设计 Android Studio 计算器布局需要综合考虑布局方式、视图属性和功能实现等方面,才能得到一个美观、实用的计算器界面。 ### 回答2: 在 Android Studio 中,我们可以使用 XML 布局文件来创建计算器布局。具体步骤如下: 1. 创建一个新的 Android 项目并命名为“Calculator”,选择空白活动类型。 2. 打开 activity_main.xml 文件,该文件是主活动的布局文件。我们可以使用 LinearLayout 来创建一个简单的计算器布局。先创建一个垂直的 LinearLayout,然后将其分为三行,每行占用一个均等的空间,最后在每行中添加所需的计算机按钮,如下所示: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1"> <Button android:text="7" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="8" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="9" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="/" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1"> <Button android:text="4" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="5" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="6" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="*" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1"> <Button android:text="1" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="2" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="3" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="-" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1"> <Button android:text="." android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="0" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="C" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <Button android:text="+" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> </LinearLayout> </LinearLayout> ``` 3. 现在我们已经创建了基本的计算器布局,但还没有添加任何逻辑来计算数字。可以在 MainActivity.java 文件中添加以下代码,将数字添加到文本视图中: ``` public class MainActivity extends AppCompatActivity { TextView textView; Button zero, one, two, three, four, five, six, seven, eight, nine, dot, equals, plus, minus, divide, multiply, clear; String value1, value2; double num1, num2; boolean addition, subtraction, multiplication, division; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView)findViewById(R.id.textView); zero = (Button)findViewById(R.id.zero); one = (Button)findViewById(R.id.one); two = (Button)findViewById(R.id.two); three = (Button)findViewById(R.id.three); four = (Button)findViewById(R.id.four); five = (Button)findViewById(R.id.five); six = (Button)findViewById(R.id.six); seven = (Button)findViewById(R.id.seven); eight = (Button)findViewById(R.id.eight); nine = (Button)findViewById(R.id.nine); dot = (Button)findViewById(R.id.dot); equals = (Button)findViewById(R.id.equals); plus = (Button)findViewById(R.id.plus); minus = (Button)findViewById(R.id.minus); divide = (Button)findViewById(R.id.divide); multiply = (Button)findViewById(R.id.multiply); clear = (Button)findViewById(R.id.clear); zero.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"0"); } }); one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"1"); } }); two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"2"); } }); three.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"3"); } }); four.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"4"); } }); five.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"5"); } }); six.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"6"); } }); seven.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"7"); } }); eight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"8"); } }); nine.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"9"); } }); dot.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(textView.getText()+"."); } }); plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (textView == null) { textView.setText(""); } else { value1 = textView.getText().toString(); addition = true; textView.setText(""); } } }); minus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (textView == null) { textView.setText(""); } else { value1 = textView.getText().toString(); subtraction = true; textView.setText(""); } } }); multiply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (textView == null) { textView.setText(""); } else { value1 = textView.getText().toString(); multiplication = true; textView.setText(""); } } }); divide.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (textView == null) { textView.setText(""); } else { value1 = textView.getText().toString(); division = true; textView.setText(""); } } }); clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(""); } }); equals.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { value2 = textView.getText().toString(); if (addition == true) { num1 = Double.parseDouble(value1); num2 = Double.parseDouble(value2); textView.setText(num1+num2+""); addition = false; } if (subtraction == true) { num1 = Double.parseDouble(value1); num2 = Double.parseDouble(value2); textView.setText(num1-num2+""); subtraction = false; } if (multiplication == true) { num1 = Double.parseDouble(value1); num2 = Double.parseDouble(value2); textView.setText(num1*num2+""); multiplication = false; } if (division == true) { num1 = Double.parseDouble(value1); num2 = Double.parseDouble(value2); textView.setText(num1/num2+""); division = false; } } }); } } ``` 4. 我们通过 MainActivity 类来管理计算器布局。我们首先创建一个 TextView 对象来在屏幕上显示计算结果,然后创建所有的数字按钮和四个操作符按钮(+,-,*和/)。在每个按钮的点击事件中,我们将相应的数字或符号添加到文本视图中。 5. 最后,在“等于”按钮的点击事件中,我们将 value1 和 value2 转换为双精度数,并使用相应的操作符进行计算。然后将结果设置为文本视图的内容。 这样,我们就可以使用 Android Studio 创建一个简单的计算器布局,并在其上添加逻辑。 ### 回答3: Android Studio是一款非常流行的开发工具,用于开发Android应用程序。其中,计算器布局是初学者入门的一个非常重要的项目。下面我们来了解一下如何在Android Studio中实现一个简单的计算器布局。 一、新建项目 在Android Studio中新建一个项目,选择“Empty Activity”模板,然后在项目名称和包名中分别输入任意名称。点击“Finish”按钮,一个新的Android项目将会被创建。 二、创建界面布局 在“res”目录下,打开“layout”文件夹,创建一个新的XML布局文件,并将文件命名为“calculator.xml”。在该布局文件中,我们需要使用如下标签创建一个简单的计算器: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:gravity="end"/> <TableRow android:orientation="horizontal" android:weightSum="4" android:padding="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="7" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button1"/> <Button android:text="8" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button2"/> <Button android:text="9" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button3"/> <Button android:text="/" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button4"/> </TableRow> <TableRow android:orientation="horizontal" android:weightSum="4" android:padding="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="4" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button5"/> <Button android:text="5" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button6"/> <Button android:text="6" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button7"/> <Button android:text="*" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button8"/> </TableRow> <TableRow android:orientation="horizontal" android:weightSum="4" android:padding="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="1" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button9"/> <Button android:text="2" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button10"/> <Button android:text="3" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button11"/> <Button android:text="-" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button12"/> </TableRow> <TableRow android:orientation="horizontal" android:weightSum="4" android:padding="5dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="0" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button13"/> <Button android:text="." android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button14"/> <Button android:text="=" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button15"/> <Button android:text="+" android:layout_weight="1" android:layout_width="0dp" android:layout_height="52dp" android:id="@+id/Button16"/> </TableRow> </LinearLayout> 在布局中,我们使用了EditText来显示计算结果,使用TableRow和Button标签创建计算器的键,将它们嵌套在一个垂直的LinearLayout中,以便与布局工具箱中其他控件一起使用。 三、编写Java代码 在MainActivity类中编写逻辑代码,将按钮按下的值附加到EditText控件上,如下所示: public class MainActivity extends AppCompatActivity { EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText); Button button1 = (Button)findViewById(R.id.Button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editText.append("7"); } }); Button button2 = (Button)findViewById(R.id.Button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editText.append("8"); } }); //依此类推 } } 通过findViewById方法获取布局中控件的引用,在每个控件上设置OnClickListener监听器,这样当该按钮被按下时,其相应方法将被执行,将该数字附加到EditText控件的末尾。 四、运行程序 最后,在Android Studio的工具栏上单击“Run”按键来运行该程序。在设备或模拟器上展示计算器,点击按钮,能够将数字附加到EditText控件上,实现简单的加减乘除计算。 以上就是在Android Studio中实现计算器布局的步骤,希望能够对初学者有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值