开发一个安卓App-计算器-绘制界面-3

本文详细介绍了如何在Android计算器应用的布局中添加第二个红框,即清零(C)按钮。通过使用TextView控件,并设置相关属性如id、尺寸、背景色、文字内容、对齐方式等,实现清零按钮的功能。代码解析帮助理解每个属性的作用,使开发者能够清晰地了解布局构建过程。
摘要由CSDN通过智能技术生成

回顾总体目标

计算器界面

回顾上一讲内容

1、计算器界面完成分割后,由如下几个红框部分堆叠而成

2、上一讲介绍了如何完成第一个红框,最终效果图

本讲内容

本讲介绍如何完成第二个红框

其实看过了前一讲的内容,这个就比较简单了,其实也就是往红框中添加一个文本显示控件,内容设置为字符串C。
好啦,看看具体的代码。

<TextView
    android:id="@+id/clearBtn"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="15dp"
    android:layout_marginEnd="15dp"
    android:background="#0a0a0a"
    android:gravity="center"
    android:text="C"
    android:textColor="#FFFFFF"
    android:textSize="30sp" />

代码解析:

  • 第一行,TextView表示添加一个文本控件,用于清除计算结果。
  • 第二行,给整个控件设置一个id:clearBtn,方便后面代码里找到整个控件,让用户点击时能有反应。
  • 第三行和第四行,表示当前控件宽和高,和父控件保持一致,也就是把父控件撑满。
  • 第五行,设置当前控件距离父控件的左边距。
  • 第六行,设置当前控件距离父控件的右边距。
  • 第七行,background属性,用来设置控件的背景颜色。
  • 第八行,gravity用来设置文字显示时的对齐方式,center表示居中。
  • 第九行,text设置显示的文字信息。
  • 第十行,textColor设置文字颜色。
  • 第十一行,textSize设置字体大小。

页面完整代码

<?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:background="#EE181818"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp">

        <TextView
            android:id="@+id/resultTv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:background="#0a0a0a"
            android:gravity="end|bottom"
            android:padding="10dp"
            android:textColor="#FFFFFF"
            android:textSize="25sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp">

        <TextView
            android:id="@+id/clearBtn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:background="#0a0a0a"
            android:gravity="center"
            android:text="C"
            android:textColor="#FFFFFF"
            android:textSize="30sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="75dp"></LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="75dp"></LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="75dp"></LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="75dp"></LinearLayout>

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值