Android UI控制項系列:LinearLayout(線性布局)

來源:互聯網
上載者:User
LinearLayout是線上性方向顯示View元素的一個ViewGroup,可以是水平方向,也可以是垂直方向

你可以重複使用LinearLayout,如果你想使用嵌套多層的LinearLayout的話,你可以考慮使用RelativeLayout來替換.

1、開始建立一個工程名字叫做HelloLinearLayout

2、開啟res/layout/main.xml檔案並且插入如下內容

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >        <LinearLayout                android:orientation="horizontal"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="1">                <TextView                        android:text="red"                        android:gravity="center_horizontal"                        android:background="#aa0000"                        android:layout_width="wrap_content"                        android:layout_height="fill_parent"                        android:layout_weight="1"                />                <TextView                        android:text="green"                        android:gravity="center_horizontal"                        android:background="#00aa00"                        android:layout_width="wrap_content"                        android:layout_height="fill_parent"                        android:layout_weight="1"                />                <TextView                        android:text="blue"                        android:gravity="center_horizontal"                        android:background="#0000aa"                        android:layout_width="wrap_content"                        android:layout_height="fill_parent"                        android:layout_weight="1"                />                <TextView                        android:text="yellow"                        android:gravity="center_horizontal"                        android:background="#aaaa00"                        android:layout_width="wrap_content"                        android:layout_height="fill_parent"                        android:layout_weight="1"                />        </LinearLayout>        <LinearLayout                android:orientation="vertical"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="1">                <TextView                        android:text="row one"                        android:textSize="15pt"                        android:layout_width="fill_parent"                        android:layout_height="wrap_content"                        android:layout_weight="1"                />                <TextView                        android:text="row two"                        android:textSize="15pt"                        android:layout_width="fill_parent"                        android:layout_height="wrap_content"                        android:layout_weight="1"                />                <TextView                        android:text="row three"                        android:textSize="15pt"                        android:layout_width="fill_parent"                        android:layout_height="wrap_content"                        android:layout_weight="1"                />                <TextView                        android:text="row four"                        android:textSize="15pt"                        android:layout_width="fill_parent"                        android:layout_height="wrap_content"                        android:layout_weight="1"                />        </LinearLayout></LinearLayout>

仔細檢查這個XML檔案。有一個根項目LinearLayout定義了它的方向是垂直的,所有的子View(一共有2個)都是被垂直方向堆起的,第一個子孩子是另一個以水平方向布局的LinearLayout,並且第二個子孩子是一個用垂直方向布局的LinearLayout,這些每一個被嵌套的LinearLayout都包含幾個TextView元素,它們的方向是由父LinearLayout標籤所定義。

3、現在開啟HelloLinearLayout.java並且確定它已經在onCreate()方法中載入了res/layout/main.xml布局檔案

public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);

setContentView(int)方法為Activity載入了布局檔案,由資源resource ID所指定—R.layout.main指的是res/layout/main.xml布局檔案

4、運行程式,你可以看到如下的情況

以上就是Android UI控制項系列:LinearLayout(線性布局)的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.