Android studio 实现计算机界面

大家好!今天我将向大家介绍如何在Android Studio中创建一个漂亮的计算器布局。通过这个教程,你将学会如何利用Android Studio的功能来设计用户界面,从而实现一个令人印象深刻的计算器界面。

首先,我们来看一下最终的效果图。在这个计算器界面中,我们使用了LinearLayout来排列各个元素,并利用权重(weight)属性来确定元素的大小。

接下来,让我们来看一下代码块(.xml文件)。这段代码是实现计算器界面的全部内容。

<?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">
    <!-- 显示计算结果的文本视图 -->
    <TextView
        android:id="@+id/txt1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></TextView>
    <!-- 包含所有按钮的布局,水平排列 -->
    <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="3"
            android:orientation="vertical">
            <!-- 第一行按钮,包含CE、除法和乘法按钮 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_CE"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="CE"></Button>
                <Button
                    android:id="@+id/btn_CHU"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="÷"></Button>
                <Button
                    android:id="@+id/btn_CHENG"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="×"></Button>
            </LinearLayout>
            <!-- 第二行按钮,包含7、8、9数字按钮 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_7"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="7"></Button>
                <Button
                    android:id="@+id/btn_8"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="8"></Button>
                <Button
                    android:id="@+id/btn_9"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="9"></Button>
            </LinearLayout>
            <!-- 第三行按钮,包含4、5、6数字按钮 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_4"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="4"></Button>
                <Button
                    android:id="@+id/btn_5"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="5"></Button>
                <Button
                    android:id="@+id/btn_6"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="6"></Button>
            </LinearLayout>
            <!-- 第四行按钮,包含1、2、3数字按钮 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="1"></Button>
                <Button
                    android:id="@+id/btn_2"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="2"></Button>
                <Button
                    android:id="@+id/btn_3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="3"></Button>
            </LinearLayout>
            <!-- 最后一行的按钮,包含0和小数点按钮 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button
                    android:id="@+id/btn_0"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:text="0"></Button>
                <Button
                    android:id="@+id/btn_DIAN"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="."></Button>
            </LinearLayout>
        </LinearLayout>
        <!-- 包含运算符按钮的布局,垂直排列 -->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
            <!-- 包含C、加法、减法和等于按钮的布局 -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <Button
                    android:id="@+id/btn_C"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:text="C"></Button>
                <Button
                    android:id="@+id/btn_JIA"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:text="+"></Button>
                <Button
                    android:id="@+id/btn_JIAN"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:text="-"></Button>
                <Button
                    android:id="@+id/btn_DENGYU"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="2"
                    android:text="="></Button>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

最后,希望这个教程对大家有所帮助!

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值