Android Studio实现一个简易的计算器布局

首先,在layout文件夹下建立一个activity_main.xml文件,并写入以下代码:

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

    <EditText
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="117dp"
        android:enabled="false"
        android:textColor="#66ccff"
        android:textSize="40sp"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/cls"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="C"
            android:background="@drawable/round_corner"
            android:textColor="#ffffff"
            android:backgroundTint="#FFA500"

            android:textSize="40sp" />

        <Button
            android:id="@+id/div"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="#FFA500"
            android:text="/"
            android:textColor="#ffffff"
            android:textSize="40sp" />

        <Button
            android:id="@+id/mul"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp"
            android:text="*"
            android:backgroundTint="#FFA500"
            android:textColor="#ffffff"/>

        <Button
            android:id="@+id/Backspace"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Backspace"
            android:backgroundTint="#FFA500"
            android:textAllCaps="false"
            android:textColor="#ffffff"

            android:textSize="20sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/seven"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp"
            android:text="7"
            android:backgroundTint="@android:color/background_dark"
            android:textColor="#ffffff"/>
        <Button
            android:id="@+id/eight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp"
            android:text="8"
            android:backgroundTint="@android:color/background_dark"
            android:textColor="#ffffff"/>
        <Button
            android:id="@+id/nine"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp"
            android:text="9"
            android:backgroundTint="@android:color/background_dark"
            android:textColor="#ffffff"/>
        <Button
            android:id="@+id/sub"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp"
            android:text="-"
            android:backgroundTint="#FFA500"
            android:textColor="#ffffff"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/four"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp"
            android:text="4"
            android:backgroundTint="@android:color/background_dark"
            android:textColor="#ffffff"/>
        <Button
            android:id="@+id/five"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp"
            android:text="5"
            android:backgroundTint="@android:color/background_dark"
            android:textColor="#ffffff"/>
        <Button
            android:id="@+id/six"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp"
            android:text="6"
            android:backgroundTint="@android:color/background_dark"
            android:textColor="#ffffff"/>
        <Button
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="40sp"
            android:text="+"
            android:backgroundTint="#FFA500"
            android:textColor="#ffffff"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/one"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="40sp"
                    android:text="1"
                    android:backgroundTint="@android:color/background_dark"
                    android:textColor="#ffffff"/>

                <Button
                    android:id="@+id/two"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="2"
                    android:textColor="#ffffff"
                    android:backgroundTint="@android:color/background_dark"
                    android:textSize="40sp" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_weight="1">
                <Button
                    android:id="@+id/zero"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="0"
                    android:textSize="40sp"
                    android:backgroundTint="@android:color/background_dark"
                    android:textColor="#ffffff"/>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">

                <Button
                    android:id="@+id/three"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:backgroundTint="@android:color/background_dark"
                    android:text="3"
                    android:textColor="#ffffff"
                    android:textSize="40sp" />

                <Button
                    android:id="@+id/spot"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:backgroundTint="#FFA500"
                    android:text="."
                    android:textColor="#ffffff"
                    android:textSize="40sp" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">
                <Button
                    android:id="@+id/equal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="="
                    android:textSize="40sp"
                    android:backgroundTint="#FFA500"
                    android:textColor="#ffffff"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

之后,把themes文件改成以下形式:

 

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

为了实现圆角按钮,首先在drawable下创建round_corner.xml文件,并写入以下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="800dp"/>
    <solid android:color="#66ccff" />
</shape>

最后实现的计算器样子:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值