Android开发UI布局之计算机练习

一、样图

二、代码

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--计算器布局练习-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <TextView
            android:layout_width="0dp"
            android:text="C"
            android:textSize="30dp"
            android:background="@drawable/rectangle"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:text="+/-"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:text="%"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:background="@drawable/rectangle_orange"
            android:text="÷"
            android:textSize="30dp"
            android:gravity="center"

            android:layout_weight="1"
            android:layout_height="match_parent"/>

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

        <TextView
            android:layout_width="0dp"
            android:text="1"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:text="2"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:text="3"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:background="@drawable/rectangle_orange"
            android:text="X"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

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

        <TextView
            android:layout_width="0dp"
            android:text="4"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:text="5"
            android:textSize="30dp" />

        <TextView
            android:layout_width="0dp"
            android:text="6"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:background="@drawable/rectangle_orange"
            android:text="--"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

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

        <TextView
            android:layout_width="0dp"
            android:text="7"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:text="8"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:text="9"
            android:textSize="30dp"
            android:gravity="center"
            android:background="@drawable/rectangle"
            android:layout_weight="1"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:background="@drawable/rectangle_orange"
            android:text="+"
            android:textSize="30dp"
            android:gravity="center"

            android:layout_weight="1"
            android:layout_height="match_parent"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="2"
            android:text="0"
            android:textSize="30sp"
            android:paddingLeft="42dp"
            android:gravity="center_vertical"
            android:background="@drawable/rectangle"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="."
            android:textSize="30sp"
            android:background="@drawable/rectangle"
            android:gravity="center"
            android:layout_height="match_parent"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:background="@drawable/rectangle_orange"
            android:text="="
            android:textSize="30sp"
            android:gravity="center"
            android:layout_height="match_parent"/>
    </LinearLayout>

</LinearLayout>

三、效果图

 四、总结心得

  这种简单的表格类型的结构可以通过两个线性布局的嵌套来实现,加边框需要在drawable里面绘制背景,这种简单的几何图形可以直接用shape,选择就行,背景(solid)边界线(stoke)。绘制好背景之后再textview里面@就可以了。gravity可以调整字在组块中的位置。

原文地址:https://www.cnblogs.com/suanai/p/12245725.html