安卓開發學習------2.線性佈局(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"
    tools:context=".MainLinearComplexActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="100dp"
        android:textColor="#00E800"
        android:gravity="right"
        android:text="0" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:text="9"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:textColor="#ff0000"
            android:textSize="40dp"
            />
        <Button
            android:text="8"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="7"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="*"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="C"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:text="6"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:textColor="#ff0000"
            android:textSize="40dp"
            />
        <Button
            android:text="5"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="4"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="/"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="40dp"
            android:layout_weight="1"
            />
        <Button
            android:text="back"
            android:layout_width="80dp"
            android:layout_height="150dp"
            android:textColor="#ff0000"
            android:textSize="19dp"
            android:layout_weight="1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:orientation="vertical">
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                >
                <Button
                    android:text="3"
                    android:layout_width="80dp"
                    android:layout_height="150dp"
                    android:textColor="#ff0000"
                    android:textSize="40dp"
                    android:layout_weight="1"
                    />
                <Button
                    android:text="2"
                    android:layout_width="80dp"
                    android:layout_height="150dp"
                    android:textColor="#ff0000"
                    android:textSize="40dp"
                    android:layout_weight="1"
                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                >
                <Button
                    android:text="0"
                    android:layout_width="80dp"
                    android:layout_height="150dp"
                    android:textColor="#ff0000"
                    android:textSize="40dp"
                    android:layout_weight="1"
                    />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            >
            <Button
                android:text="1"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
            <Button
                android:text="."
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            >
            <Button
                android:text="-"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
            <Button
                android:text="+"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            >
            <Button
                android:text="="
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textColor="#ff0000"
                android:textSize="40dp"
                android:layout_weight="1"
                />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章