Android小项目——仿iPhone计算器

前言

一个模仿iPhoneUI的计算器,支持大数运算,带括号的运算。

界面设计

界面展示

请添加图片描述
在这里插入图片描述

如图所示,为该计算器UI界面。主要采用LinearLayout布局。并且在其中嵌套LinearLayout

整体框架

首先,在整体框架内添加一下几种属性:

  • 设置背景颜色为黑色
  • 定义orientation为垂直布局
  • 设置fitsSystemWindowstrue,让状态栏可改变
<LinearLayout 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:background="#000"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

这里还设置了一个取消标题栏的效果。只需在AndroidManifest.xml文件内的onCreate()方法内加入以下代码即可

	 ActionBar actionBar = getSupportActionBar();//隐藏标题栏
        if (actionBar != null) {
   
            actionBar.hide();
        }

并且,我们发现状态栏也变成了透明色,这里我们直接在setContentView()前添加一段代码即可。代码如下:

  if (Build.VERSION.SDK_INT >= 21) {
   //状态栏透明
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

如此整体布局大概完成。

按钮布局

选择在LinearLayout里面嵌套LinearLayout

  • 输入框
    这里输入显示框选择了一个字体自适应的属性。android:autoSizeTextType="uniform感兴趣的朋友可以了解一下,但是一旦使用了这个属性,我们在TextView里设置的hint就会失效。
  • 按钮
    这里的按钮实现了一个点击变色的效果。方法很简单,只需在drawavle文件下,先创建按前和按后的状态的xml文件。接着再新建一个xml文件将这两种状态引入。用android:state_pressed:true控制按钮变色状态。最后将文件引入activity_main并在每一个按钮添加android:background=""将各自的按钮变化引入。
    代码如下:
  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_gravity="bottom"
            android:layout_marginRight="20dp"
            android:autoSizeMaxTextSize="90dp"
            android:autoSizeTextType="uniform"
            android:gravity="bottom|right"
            android:hint="0"
            android:maxLines="3"
            android:textColor="#fff"
            android:textColorHint="#fff" />
    </LinearLayout>

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

        <Button
            android:id="@+id/button_AC"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_shape1_play"
            android:text="AC"
            android:textColor="#000"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button_left_bracket"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_shape1_play"
            android:text="("
            android:textColor="#000"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button_right_bracket"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_shape1_play"
            android:text=")"
            android:textColor="#000"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button_div"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_shape2_play"
            android:text="÷"
            android:textColor="#fff"
            android:textSize="30dp" />
    </LinearLayout>

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

        <Button
            android:id="@+id/button_7"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_shape3_play"
            android:text="7"
            android:textColor="#FFF"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button_8"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_shape3_play"
            android:text="8"
            android:textColor="#FFF"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button_9"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_shape3_play"
            android:text="9"
            android:textColor="#FFF"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button_mul"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_shape2_play"
            android:text="×"
            android:textColor="#fff"
            android:textSize="35dp" />
    </LinearLayout>

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

        <Button
            android:id="@+id/button_4"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_shape3_play"
            android:text="4"
            android:textColor="#FFF"
            android:textSize="30dp" />

        <Button
            android:id="@+id/button_5"
            android:layout_width="70dp"
            android:layout_height="70dp
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值