1. 程式人生 > >Android第三講——五大布局(UI)

Android第三講——五大布局(UI)

Android的五大布局

UI:View與ViewGroup物件建立的UI
ViewGroup是包含多個View與ViewGroup的容
器ViewGroup繼承於View
巢狀層次不要超過10層,否則低執行效率

px畫素
dp一英寸上存在160px 那麼1dp=1px 一英寸/160,手機不同解析度不同dp不同
sp 文字的大小跟dp一樣,只用於文字的大小

線性佈局LinearLayout

只能是單列或者單行
沒有設定orientation預設horizontal(水平)
vertical(垂直)
gravity:表示控制元件所處的位置

<?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:gravity="bottom"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text
="Button1"/>
</LinearLayout>

這裡寫圖片描述
layout_gravity相對於父控制元件

     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:layout_gravity="center_horizontal"/><!-- 表示此按鈕處於相對於父控制元件的水平居中-->

layout_weight所佔比重

 <Button
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Button1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:layout_gravity="center_horizontal"/>

這裡寫圖片描述

練習,寫出如下佈局:
這裡寫圖片描述
1、

<?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">
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

2、

<?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">
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />
</LinearLayout>

3、需要LinearLayout巢狀

<?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">
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="2"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2"/>
        </LinearLayout>
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

4、需要LinearLayout巢狀

<?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="0dp"
        android:layout_weight="1">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
        </LinearLayout>
    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
        </LinearLayout>
        <Button
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

相對佈局RelativeLayout

alignParentLeft Right Bottom Top相對父控制元件的上下左右
centerInParent centerVertical
centerHorizontal相對父控制元件的中心

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="Button1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Button2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Button3"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Button4"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Button5"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="Button6"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button7"/>
</RelativeLayout>

如圖:
這裡寫圖片描述

toLeftOf toRightOf above below相對後邊跟的id的控制元件的上下左右

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/button_1"
        android:text="Button2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/button_1"
        android:text="Button3"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/button_1"
        android:text="Button4"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button_1"
        android:text="Button5"/>
</RelativeLayout>

如圖:
這裡寫圖片描述

alignLeft alignRight alignBottom alignTop相對有空間的上下左右邊對齊

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/button_1"
        android:text="Button2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/button_1"
        android:text="Button4"/>
</RelativeLayout>

如圖:
這裡寫圖片描述
alignBaseline基準線對齊

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/button_1"
        android:text="Button2"/>
</RelativeLayout>

如圖:
這裡寫圖片描述

幀佈局FrameLayout

visibility()屬性;visible顯示 invisible不顯示但是佔用位置 gone直接去掉
可以疊加.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        />
    <Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:textAllCaps="false"
        android:text="@string/button2"/>
</FrameLayout>

這裡寫圖片描述此時兩個Button都可見

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:visibility="invisible"
        />
    <Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:textAllCaps="false"
        android:text="@string/button2"/>
</FrameLayout>

這裡寫圖片描述此時最下邊的Button不可見,但是它還佔用著位置

表格佈局TableLayout

一般的:首先要有TableRow

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3"/>
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3"/>
    </TableRow><TableRow>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3"/>
</TableRow>
</TableLayout>

這裡寫圖片描述
collapseColumns隱藏某一列
TableLayout內加入此句:0表示第一列,依次類推

android:collapseColumns="0"

這裡寫圖片描述 此時隱藏了第一列。
shrinkColumns自動收縮某一列,防止其他列被擠出去

android:shrinkColumns="0"
<!--將Button1改為 -->
 <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1sfdsaaaaaaaaaaaaaadsffffffffff"/>

這裡寫圖片描述
layout_span表示佔幾列

<!--在Button1中加入layout_span-->
<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            <!--"2"表示此Button2-->
            android:layout_span="2"
            android:text="Button1"/>

這裡寫圖片描述 此時Button1佔兩列。

絕對佈局AbsoluteLayout

·不推薦使用
直接隨意拖拽。