LinearLayout (线性布局)的分析
android提供了5中布局,線性布局,相對布局,幀布局,表格布局和絕對布局
線性和相對布局用的是最多的
下面要說的是線性布局
提到線性布局 一定要記住,它里面的所有組件一定不會重疊的,
切不會換行,當組件排列到窗體的邊緣后,后面的組件不會顯示不來。
線性布局是將放入其中的組件按照水平或者垂直方向來布局的,
?線性布局的語法:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
? ?屬性列表
</LinearLayout>
簡單的寫一個:
<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"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#66ff66"android:textSize="25sp"android:text="面碼" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="25sp"android:background="#ff0000"android:text="小木" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="25sp"android:background="#ffcc33"android:text="小桃" /> </LinearLayout>效果圖:
這個是垂直方向排列的,
線性布局中常用的屬性:
?android:orientation 指定線性布局排列的方向,其值有vertical是垂直horizontal是水平,
?andorid:gravity 布局管理器內組件的對其方法,上下左右,這個前面已經說了,這里
在提起是想說當同時指定多個屬性的時候中間用|分開 例如left|bottom.
android:layout_width 指定組件的寬度,其值有fill_parent,match_parent,wrap_content,
其中fill_parent和match_parent作用相同,表示組件的寬度與父容器的寬度相同。
(android2.2之后推薦使用match_parent)
<TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#66ff66"android:textSize="25sp"android:text="面碼" />例如這個textview 的寬度就是和手機屏幕的寬度一樣,
wrap_content表示該組件的寬度恰好和它的寬度一樣
例如上面textview的高度,這個高度和字體的高度一樣的。
android:layout_height 指定組件的高度,里面的值和寬度一樣
android:id 指定當前組件的一個id屬性, 這個指定之后android會在
R.java中自動生成唯一的id值,
android:weight 權重,這個就不再說了,不理解的參考
http://blog.csdn.net/qq_33210042/article/details/50907811
android:background 指定組件的背景, 這個要說下它的指定方法
1 直接使用顏色值就像我上面的代碼android:background="#ffcc33"
2 調用圖片anroid:background="drawable/圖片的名字"
3 使用android系統自帶的?android:background="@android:color/white"
android:visibility 指定布局中的組件是否顯示,gone 隱藏,visible顯示,
invisible不顯示但贊內存
下面說一個線性布局的技巧(這個老師教的):
先看代碼:
<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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#66ff66"android:textSize="25sp"android:layout_gravity="right"android:text="面碼" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25sp"android:background="#ff0000"android:layout_gravity="left"android:text="小木" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25sp"android:background="#ffcc33"android:layout_gravity="top"android:text="小桃" /> <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="25sp"android:background="#ffcc33"android:layout_gravity="center"android:text="小桃" /> </LinearLayout>
看圖能發現top沒有效果了, 設置成bottom一樣沒有效果
這里總結下:
線性布局 是豎直方向是,左右對齊有效,頂部底部對齊無效,水平
居中生效,豎直居中無效,
在水平方向上則是反過來的,有興趣的童鞋可以試試。
總結
以上是生活随笔為你收集整理的LinearLayout (线性布局)的分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android weight(权重)的具
- 下一篇: android Style(样式)的解析