1. 程式人生 > >安卓線性佈局(屬性)

安卓線性佈局(屬性)

安卓六大布局:

AbsoluteLayout  絕對佈局(已過期,不建議使用)

RelativeLayout    相對佈局

LinearLayout       線性佈局

FrameLayout       幀佈局

TableLayout        表格佈局 (是LinerLayout的子類)

GridLayout            網格佈局(4.0推出)

安卓目前使用的佈局UI控制元件網狀圖:(該圖箭頭所指向的是其父類)



目前推薦使用RelativeLayoutLinearLayout;FrameLayout三個佈局

<1>線性佈局    LinearLayout佈局

線性佈局控制其中的控制元件按照橫向或縱向方式排列,並且線性佈局不會換行,當控制元件

排列到窗體邊緣,後面的控制元件就被隱藏,不會被顯示出來。

線性佈局的預設方向是水平方向(Hoizontal)

線性佈局的屬性:      match parent :匹配父容器的大小

                             wrap content :根據容器內的東西決定元件的大小

                             android:orientation(確定方向):定義佈局內控制元件或元件的排列方式  vertical(垂直)horizontal(水平)

                             android:background: 設定控制元件的背景顏色或背景圖片

                             android:id                設定控制元件的Id。這樣就可以在R.java中生成相應的值,在程式中通過findViewById就可以呼叫                                                    android:id = "@+id/id的名字"

                             gravity                        android:gravity="center":指定當前控制元件中內容(子控制元件)的對齊方式   

                                                               android:layout_gravity="center":當前控制元件在其父控制元件中的對齊方式

    程式碼註釋:

<?xml version="1.0" encoding="UTF-8"?>

<!-- LinearLayout:線性佈局,只能沿著水平或者垂直一個方向擺放控制元件,預設是沿著水平方向擺放控制元件 android:orientation="horizontal":水平方向,只能沿著水平方向擺放控制元件(一列只能放一個控制元件),是預設值 android:orientation="vertical":垂直方向,只能沿著垂直方向擺放控制元件(一行只能放一個控制元件) android:layout_width="match_parent" -->

-<LinearLayout android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<!-- android:layout_weight="1":權重(比例),如果不設定,權重值預設為0,預設情況下,手機渲染控制元件時,會首先 顯示權重值小的控制元件,然後在顯示稍大一點的控制元件 當線性佈局的方向為垂直方向時,如果指定控制元件的高度是match_parent,則此時指定控制元件的權重值和其佔據空間大小成反比. 當線性佈局的方向為垂直方向時,如果指定控制元件的高度是wrap_content,則此時指定控制元件的權重值和其佔據空間大小成正比. -->


<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="One" android:textSize="30sp" android:background="@color/colorAccent" android:layout_weight="1"/>

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Two" android:textSize="30sp" android:background="@color/colorPrimary" android:layout_weight="2"/>

<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Three" android:textSize="30sp" android:background="@android:color/holo_red_dark" android:layout_weight="3"/>

</LinearLayout>

TextView:文字顯示控制元件

android:text:顯示提示資訊文字

EditText:文字輸入控制元件,可以讓使用者輸入自己的內容

android:hint=“請輸入使用者名稱”:當用戶沒有輸入任何內容時顯示的提示文字,使用者一旦輸入則提示文字自動消失,刪除輸入的字元後提示文字自動顯示

android:id = "@+id/editText_userName"  :將新增R類中的editText_userName靜態內部類中的userName靜態常量