android基礎——介面布局(二) 線性布局

來源:互聯網
上載者:User

LinerLayout 是一行(列)只能放置一個控制項 的線性布局,所以當有很多控制項需要列出來的時候,就可以用這個拉。

代碼:

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >        <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:textSize="15pt"        android:background="#aa0000"        android:layout_weight="1"        android:text="@string/hang1"        />        <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:textSize="15pt"        android:background="#00aa00"        android:layout_weight="1"        android:text="@string/hang2"        />        <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:textSize="15pt"        android:background="#0000aa"        android:layout_weight="1"        android:text="@string/hang3"        />        <TextView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center_vertical"        android:textSize="15pt"        android:background="#aaaa00"        android:layout_weight="1"        android:text="@string/hang4"        />    </LinearLayout>

string.xml中的代碼

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AndroidBookLinerLayoutp101Activity!</string>
<string name="app_name">垂直線性布局</string>
<string name="hang1">第一行</string>
<string name="hang2">第二行</string>
<string name="hang3">第三行</string>
<string name="hang4">第四行</string>
</resources>

解析:

orientation="vertical"表示這個是垂直線性布局

fill_parent 表示該控制項填充整個螢幕

wrap_content表示該控制項隨文字欄位的不同而改變寬度或高度

gravity指明所在位置,上下左右中等等

layout_weight:預設值為0,表示需要多大的視圖就佔據多大的空間; 大於0,則需要按父類別檢視的可用空間進行分割,分割大小取決於該值的大小和在當前所佔的比例 

android:text="@string/hang1":引用string裡面的hang1文本。這個很容易理解的了。就這樣規定的

 android:background="#aa0000"背景色

把上面的垂直改為 android:orientation="horizontal"水平,就出現這個效果了

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.