Android線性布局LinearLayout(七)

來源:互聯網
上載者:User

標籤:android線性布局linearlay

一、先瞭解幾個屬性:

1.布局

1) android:orientation="vertical" 垂直布局 

2)android:orientation="horizontal"  水平布局


2.控制項內容高和寬布局

1) android:layout_width="match_parent" ,布滿整個螢幕. [匹配父視窗]

2)android:layout_height="wrap_content",布局元素將根據內容更改大小.[內容包括]


3.布局比例

所有的視圖都有一個layout_weight值,預設為零,意思是需要顯示 多大的視圖就佔據多大的螢幕空 間。若賦一個高於零的值,則將父視 圖中的可用空間分割,分割大小具體取決於每一個視圖的layout_weight 值以及該值在當前螢幕布局的整體 layout_weight值和在其它視圖螢幕布 局的layout_weight值中所佔的比率而定。


二、下面兩個例子實現:

1)三個按鈕橫向擺放


  <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="mc" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="m+" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="m-" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="mr" />    </LinearLayout>
借用layout_weight使三個按鈕擺放佔用比例一致.
二、兩層按鈕:第一層三個,第二次2個


<LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >                <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="3"            android:orientation="horizontal" >            <Button                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="1" />            <Button                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="2" />            <Button                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="3" />        </LinearLayout>                   <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="3"            android:orientation="horizontal" >                              <Button                android:layout_width="0px"                android:layout_height="wrap_content"                android:layout_weight="2"                android:text="0" />            <Button                android:layout_width="0px"                android:layout_height="wrap_content"                android:layout_weight="1"                android:text="." />             </LinearLayout>    </LinearLayout>
先用一個大層,設定權重比例都是3. 第二層的第一個按鈕比例的權重又占它屬於層的2/3.

Android線性布局LinearLayout(七)

聯繫我們

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