LinearLayout(线性布局)
生活随笔
收集整理的這篇文章主要介紹了
LinearLayout(线性布局)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
要點:
android:orientation="vertical"垂直線性布局,"horizontal"水平線性布局
android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制布局中控件的對齊方式。如果是沒有子控件的控件設(shè)置此屬性,表示其內(nèi)容的對齊方式,比如說TextView里面文字的對齊方式;若是有子控件的控件設(shè)置此屬性,則表示其子控件的對齊方式,gravity如果需要設(shè)置多個屬性值,需要使用“|”進行組合
android:gravity 與 android:layout_gravity的區(qū)別
android:gravity是指定本元素的子元素相對它的對齊方式。
android:layout_gravity是指定本元素相對它的父元素的對齊方式。
android:orientation="vertical"垂直線性布局,"horizontal"水平線性布局
android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制布局中控件的對齊方式。如果是沒有子控件的控件設(shè)置此屬性,表示其內(nèi)容的對齊方式,比如說TextView里面文字的對齊方式;若是有子控件的控件設(shè)置此屬性,則表示其子控件的對齊方式,gravity如果需要設(shè)置多個屬性值,需要使用“|”進行組合
android:gravity 與 android:layout_gravity的區(qū)別
android:gravity是指定本元素的子元素相對它的對齊方式。
android:layout_gravity是指定本元素相對它的父元素的對齊方式。
android:layout_weight="1"通過設(shè)置控件的layout_weight屬性以控制各個控件在布局中的相對大小,線性布局會根據(jù)該控件layout_weight值與其所處布局中所有控件layout_weight值之和的比值為該控件分配占用的區(qū)域。在水平布局的LinearLayout中有兩個Button,這兩個Button的layout_weight屬性值都為1,那么這兩個按鈕都會被拉伸到整個屏幕寬度的一半。如果layout_weight指為0,控件會按原大小顯示,不會被拉伸;對于其余l(xiāng)ayout_weight屬性值大于0的控件,系統(tǒng)將會減去layout_weight屬性值為0的控件的寬度或者高度,再用剩余的寬度或高度按相應(yīng)的比例來分配每一個控件顯示的寬度或高度。
如:
代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".LinearLayoutActivity" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:orientation="horizontal" ><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#aa0000"android:gravity="center_horizontal|center_vertical"android:text="第一列"android:textSize="15sp" ></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#00aa00"android:gravity="center_horizontal"android:text="第二列"android:textSize="15sp" ></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#0000aa"android:gravity="center|bottom"android:text="第三列"android:textSize="15sp" ></Button><Buttonandroid:layout_width="wrap_content"android:layout_height="match_parent"android:layout_weight="1"android:background="#aaaa00"android:gravity="bottom"android:text="第四列"android:textSize="15sp" ></Button></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical" ><Buttonandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="bottom"android:text="第1行"android:textSize="15sp" ></Button><Buttonandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="bottom"android:text="第2行"android:textSize="15sp" ></Button><Buttonandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="bottom"android:text="第3行"android:textSize="15sp" ></Button><Buttonandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="bottom"android:text="第4行"android:textSize="15sp" ></Button></LinearLayout></LinearLayout>總結(jié)
以上是生活随笔為你收集整理的LinearLayout(线性布局)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android TextView中文字通
- 下一篇: JFreeChart 使用介绍