1. 程式人生 > 其它 >Android添加布局和按鍵

Android添加布局和按鍵

Android添加布局和按鍵

Android佈局方式分為

    1.LinearLayout (線性佈局)

    2.ConstraintLayout (約束佈局)

    3.FrameLayout (幀佈局)

    4.TableLayout (表格佈局)

    5.RecyclerView (迴圈使徒)

我拿LinearLayout 線性佈局來做演示,推薦書籍 《基於android studio的app案列教程(第2版)—宋三華》 講的很詳細,很好理解,沒有基礎也能看。 

 

首先:找到app下的activity_main.xml,雙擊開啟,

 

我們將佈局方式改為LinearLayout ,同時使用垂直方向佈局(vertical),水平方向佈局為horizontal。

 

 這時候我們得到了一個空白的線性佈局,這時候就可以在佈局中新增按鍵(設定背景看我的文章:Android新增背景和設定app圖示

 一行裡面新增兩個Button按鍵,新增三個按鍵就是在LinearLayout之中含有三個Button

 

 

將以下程式碼儲存到activity_main.xml可直接使用,新增背景還需要自己配置 Android新增背景和設定app圖示

<?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:orientation="vertical">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="85dp">
        <Button
            android:id="@+id/button1"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭1"
            android:textSize="20dp">
        </Button>
        <Button
            android:id="@+id/button2"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭2"
            android:textSize="20dp">

        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="85dp">
        <Button
            android:id="@+id/button3"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭3"
            android:textSize="20dp"
            >
        </Button>
        <Button
            android:id="@+id/button4"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭4"
            android:textSize="20dp">

        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="85dp">
        <Button
            android:id="@+id/button5"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭5"
            android:textSize="20dp">
        </Button>
        <Button
            android:id="@+id/button6"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭6"
            android:textSize="20dp">

        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="85dp">
        <Button
            android:id="@+id/button7"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭7"
            android:textSize="20dp">
        </Button>
        <Button
            android:id="@+id/button8"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭8"
            android:textSize="20dp">

        </Button>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="85dp">
        <Button
            android:id="@+id/button9"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭9"
            android:textSize="20dp">
        </Button>
        <Button
            android:id="@+id/button10"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="饅頭10"
            android:textSize="20dp">

        </Button>
    </LinearLayout>


</LinearLayout>

  

 

很簡單,通過佈局可以隨著你的想法,新增不同的元件來構造你想要的模型