Android基于Java开发 | 1.计算器布局

写在前面

  本系列为基于Java语言的Android开发的相关学习,由于笔者只有一些web前端知识,对于安卓开发没有涉及,所以通过本项目的Android开发,学习Android应用开发的相关知识以及基础的Java语言应用,本系列博文可能在理解上,不是特别清晰,知识方面也不是特别系统,主要在学习过程中,是以目标驱动型,即学习如何实现对应目标的知识。


1 实验目标

  本次实验预期希望实现手机内计算器的相似布局,具体界面如图1所示。
计算器布局

图1 计算器布局

 本次项目的最终目标是希望实现类似于上图的计算器Android应用,本次实验则仅以实现该计算器布局为目标。

2 计算器布局

 根据Android开发者文档——Layouts一节中,包含了对于布局的介绍,其中有五种较为常见的布局类型:Linear Layout, Relative Layout, Web View, List View, Grid View。由于实在是不清楚各个布局的特点,在此也不再赘述,在Android开发者文档中,对于各布局的特点以及属性介绍有着详细的介绍。

2.1 GridLayout

 网格布局,在面对计算器的布局时,由于键盘通常呈现网格的形式,同时搜索计算器许多相关案例中,都以计算器布局作为GridLayout网格布局的经典案例,在最开始的尝试中,个人也尝试了以网格布局作为布局节点,即绘制了4列7行的网格,并在不同的网格中放置不同的控件。网格布局的代码非常简单,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="4"
    android:orientation="horizontal"
    android:rowCount="8">

    <TextView
        android:layout_columnSpan="1"
        android:layout_rowSpan="1"
        android:layout_gravity="fill"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text="菜单"
        android:textSize="30dp" />

    <TextView
        android:layout_columnSpan="4"
        android:layout_rowSpan="3"
        android:layout_gravity="fill"
        android:gravity="right"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:text="0"
        android:textSize="60dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="回退" />

    <Button
        android:layout_columnSpan="2"
        android:layout_gravity="fill"
        android:text="清空" />

    <Button
        android:layout_columnSpan="1"
        android:layout_gravity="fill"
        android:text="+" />

    <Button android:text="1" />

    <Button android:text="2" />

    <Button android:text="3" />

    <Button android:text="-" />

    <Button android:text="4" />

    <Button android:text="5" />

    <Button android:text="6" />

    <Button android:text="*" />

    <Button android:text="7" />

    <Button android:text="8" />

    <Button android:text="9" />

    <Button android:text="/" />

    <Button
        android:layout_width="wrap_content"
        android:text="." />

    <Button android:text="0" />

    <Button android:text="=" />



</GridLayout>

 补充GridLayout重要属性的介绍

<GridLayout 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="wrap_content"						# 布局宽度
    android:layout_height="wrap_content"					# 布局高度
    android:columnCount="4"									# 网格列数
    android:orientation="horizontal"						# 水平布局
    android:rowCount="8"									# 网格行数
    >									

 但是再利用GridLayout后发现布局效果差强人意,由于GridLayout布局中各个网格需要保证完全平均分配,即各网格大小一致,当屏幕大小无法满足时,会出现网格布局只占屏幕的一部分,如图2所示。
网格布局效果

图2 网格布局下的计算器布局

  如果仅仅是利用布局适配器,暂时无法实现GridLayout填满屏幕的效果,为此还是采用了LinearLayout线性绝对布局的方式。

2.2 LinearLayout

  如何利用线性布局实现类网格式,通过多个垂直、水平的线性布局进行嵌套,从而实现多行多列的布局方式,简单列举线性布局LinearLayout的属性如下:

<LinearLayout 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"							# 布局高度
    android:orientation="vertical"									# 布局分布类型:垂直分布
    android:weightSum="8" 											# 垂直节点权重之和
    >

 其中,weightSum属性即代表了该节点内,所隐含的节点权重之和,举个例子,如果父节点中的weightSum=2,若包含两个子节点,其中各个子节点中的weight=1,则最终布局效果为,两个子节点高度平均分配,垂直分布。效果如图3所示
在这里插入图片描述

图3 LinearLayout中WeightSum解释

 而如果父节点中的weightSum=3,包含两个子节点,第一个子节点A中weight=2,而第二个子节点B中weight=1,则该布局下A节点占据 2 3 \frac{2}{3} 32,而B节点占据 1 3 \frac{1}{3} 31,效果如图4所示。

在这里插入图片描述

图4 LinearLayout中WeightSum解释

 基于该属性可以划分出不同大小的区域,进行线性分布,最后设计计算器布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:weightSum="8" >

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

        <TextView
            android:layout_width="match_parent"
            android:text="菜单"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:textSize="30sp"/>

        <TextView
            android:layout_width="match_parent"
            android:text="菜单"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:textSize="30sp"/>

        <TextView
            android:layout_width="match_parent"
            android:text="菜单"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:textSize="30sp"/>


    </LinearLayout>



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

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/clear"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/back"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/mod"
            android:textSize="30sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/dev"
            android:textSize="35sp"
            android:textColor="@color/black"/>

    </LinearLayout>

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num7"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num8"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num9"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/mult"
            android:textSize="35sp"
            android:textColor="@color/black" />

    </LinearLayout>

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num4"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num5"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num6"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/sub"
            android:textSize="40sp"
            android:textColor="@color/black" />

    </LinearLayout>

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num1"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num2"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num3"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/add"
            android:textSize="30sp"
            android:textColor="@color/black" />

    </LinearLayout>

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

        <Button
            android:textAllCaps="false"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/e"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num0"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/dot"
            android:textSize="30sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/eq"
            android:textSize="30sp"
            android:textColor="@color/black" />

    </LinearLayout>

</LinearLayout>

 布局最终效果如图5所示。

计算器最终布局效果

图5 计算器最终布局效果

  以上是简易计算器布局的学习,其中控件、属性许多相关知识,受限于笔者知识水平匮乏,所以不敢随意解释,还望各位海涵。

2.3 计算器横向布局

 横向布局的控件与竖向布局并无差异,整体上只是将屏幕旋转,基于上述的LinearLayout,编写水平布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:weightSum="8" >

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

        <TextView
            android:layout_width="match_parent"
            android:text="菜单"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:textSize="30sp"/>

        <TextView
            android:layout_width="match_parent"
            android:text="菜单"
            android:layout_weight="3"
            android:layout_height="match_parent"
            android:textSize="30sp"/>


    </LinearLayout>



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

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/left_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/right_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mc"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_add"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_sub"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mr"
            android:textSize="20sp"
            android:textColor="@color/black"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/clear"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/back"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mod"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/dev"
            android:textSize="25sp"
            android:textColor="@color/black"/>

    </LinearLayout>

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


        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/left_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/right_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mc"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_add"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_sub"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_sub"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num7"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num8"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num9"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/mult"
            android:textSize="25sp"
            android:textColor="@color/black" />

    </LinearLayout>

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/left_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/right_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mc"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_add"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_sub"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mr"
            android:textSize="20sp"
            android:textColor="@color/black"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num4"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num5"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num6"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/sub"
            android:textSize="40sp"
            android:textColor="@color/black" />

    </LinearLayout>

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/left_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/right_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mc"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_add"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_sub"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mr"
            android:textSize="20sp"
            android:textColor="@color/black"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num1"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num2"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num3"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/add"
            android:textSize="30sp"
            android:textColor="@color/black" />

    </LinearLayout>

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/left_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/right_bracket"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mc"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_add"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/m_sub"
            android:textSize="20sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/mr"
            android:textSize="20sp"
            android:textColor="@color/black"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:layout_gravity="center"
            android:text="@string/num0"
            android:textSize="20sp"
            android:textColor="@color/black"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/num0"
            android:textSize="25sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/dot"
            android:textSize="30sp"
            android:textColor="@color/black" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:backgroundTint="@color/white"
            android:text="@string/eq"
            android:textSize="30sp"
            android:textColor="@color/black" />

    </LinearLayout>

</LinearLayout>

  代码效果如图6所示
计算器水平布局效果

图6 计算器水平布局效果
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值