LinearLayout-weight
LinearLayout-weight:按屏幕剩余空間仇味,按權(quán)重分配空間(權(quán)重雹顺、百分比布局)
(1)當(dāng)layoutout_height="0dp"
- 剩余空間=(屏幕高度-2*0)=屏幕高度
- 屏幕高度1/3+0= 1/3屏幕高度
- 屏幕高度2/3+0= 2/3屏幕高度
(2)當(dāng)layoutout_height="wrap_content"
- 剩余空間=(屏幕高度-2*wrap_content)
- (屏幕高度-2wrap_content)1/3+wrap_content
- (屏幕高度-2wrap_content)2/3+wrap_content
思考:如果將layout_weight=2的控件權(quán)重改為layout_weight=200贩挣,layout_weight=1的控件是否會(huì)消失不見呢没酣?不會(huì)的裕便,因?yàn)楫?dāng)layout_height為wrap_content的時(shí),layout_weight代表的是控件要優(yōu)先盡可能的小挂疆,即weight值越小下翎,控件所占的空間越小,但這個(gè)小是有限度的胆萧,即wrap_content鸳碧,也就是內(nèi)容還是要完整的展示出來犬性,所以layout_weight=1的控件不會(huì)消失腾仅,并會(huì)完整的顯示出來推励。
(3)當(dāng)layouto_height="match_parent"
- 剩余空間=(屏幕高度-2屏幕高度)=-1屏幕高度
- -1屏幕高度1/3+屏幕高度= 2/3*屏幕高度
- -1屏幕高度2/3+屏幕高度= 1/3*屏幕高度
Paste_Image.png
思考:同樣肉迫,如果將layout_weight=2的控件權(quán)重改為layout_weight=200喊衫,它是否會(huì)消失不見呢杆怕?此時(shí)在屏幕中就看不到layout_weight=2的控件了陵珍,它變成一個(gè)很小的橫條,layout_weight=1的控件充滿了整個(gè)屏幕瑟幕。這是因?yàn)?strong>當(dāng)layout_height為match_parent時(shí)留潦,layout_weight代表的是控件要優(yōu)先盡可能的大,即weight值越小鹿霸,控件所占的空間越大懦鼠,但這個(gè)大是有限度的屹堰,即match_parent。
總結(jié):
- 在layout_height設(shè)置為match_parent的時(shí)候睦袖,layout_weight所代表的是你的控件要優(yōu)先盡可能的大荣刑,但這個(gè)大是有限度的厉亏,即match_parent( layout_width同layout_height );
- 在layout_height設(shè)置為wrap_content的時(shí)候皿淋,layout_weight所代表的是你的控件要優(yōu)先盡可能的小,但這個(gè)小是有限度的疯暑,即wrap_content(layout_width同layout_height )哑舒;
- 當(dāng)沒有設(shè)置 Layout_weight這個(gè)屬性時(shí),默認(rèn)值是0乖阵。
將控件放在水平最右
1.Layout_gravity瞪浸、gravity和layout_marginRight/layout_marginLeft區(qū)別
- gravity
對該view內(nèi)容的限定吏祸。比如一個(gè)button上的text.贡翘,你可以設(shè)置該text在view的靠左,靠右等位置泛鸟。以button為例,android:gravity="right"則button上面的文字靠右踊东。 - layout_marginRight/layout_marginLeft
離某元素右邊緣/左邊緣的距離北滥。 - Layout_gravity
設(shè)置該view相對與起父view 的位置。比如一個(gè)button 在Linearlayout里闸翅,你想把該button放在靠左再芋、靠右等位置就可以通過該屬性設(shè)置。以button為例坚冀,android:layout_gravity="right"則button靠右济赎。
注意:在LinearLayout中,設(shè)置Layout_gravity屬性:
- 當(dāng)android:orientation="vertical" 時(shí)记某, 只有水平方向的設(shè)置才起作用司训,垂直方向的設(shè)置不起作用液南,即:left壳猜,right,center_horizontal是生效的贺拣;
- 當(dāng) android:orientation="horizontal" 時(shí)蓖谢, 只有垂直方向的設(shè)置才起作用,水平方向的設(shè)置不起作用譬涡,即:top闪幽,bottom,center_vertical 是生效的涡匀。
2.控件水平靠右例子:
-
兩個(gè)控件靠右顯示
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清空" />
</LinearLayout>
- 水平方向中布置兩個(gè)TextView一個(gè)左盯腌,一個(gè)右,此時(shí)借用LinearLayout的gravity陨瘩、Layout_gravity屬性等不能得到預(yù)想的結(jié)果腕够,使用RelativeLayout的android:layout_alignParentRight屬性能實(shí)現(xiàn)預(yù)期的結(jié)果。
# LinearLayout-divider
在android3.0及后面的版本在LinearLayout里增加了個(gè)分割線:
android:divider="@drawable/shape"
android:showDividers="middle|beginning|end"
分割線如果是圖片那就直接使用圖片就行舌劳,如果要使用顏色就必須使用shape來顯示帚湘,直接使用顏色或Color是沒有用的。使用shape的時(shí)候要注意設(shè)置size屬性不設(shè)置寬高分割線就不會(huì)顯示出來甚淡,如果使用line那填充顏色只能使用stroke來顯示顏色大诸。
android:showDividers="end"屬性使用要注意:在android6.0一下,畫最后一條分割線時(shí)是畫在最后一個(gè)Child下面的贯卦,當(dāng)最后一個(gè)child設(shè)置為GONE资柔,最后一條分割線就會(huì)畫到linearLayout頂部去;在6.0以上就改為畫到最后一個(gè)Visibility不為GONE的Child的下面了撵割,具體分析見[這里](http://angeldevil.me/2015/12/29/LinearLayout%E5%9D%91%E7%88%B9%E7%9A%84showDividers%E5%B1%9E%E6%80%A7/)贿堰。
這里重點(diǎn)說下LinearLayout的虛線divider的使用以及遇到的問題。一般步驟如下:
###### 1.先在drawable目錄下創(chuàng)建一個(gè)線line的shape文件
比如此divider_dash.xml文件代碼:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:right="5dp"
android:left="5dp">
<shape android:shape="line">
<stroke
android:width="1dp"
android:color="#E5E5E5"
android:dashGap="4dp"
android:dashWidth="3dp"/>
<size android:height="2dp" />
</shape>
</item>
</layer-list>
這里畫線必須要注意是:
- 只能畫水平線啡彬,畫不了豎線羹与;
- 線在整個(gè)形狀區(qū)域中是居中顯示的;
- 線左右兩邊會(huì)留有空白間距庶灿,線越粗注簿,空白越大;
- 線的高度是通過stroke的android:width屬性設(shè)置的跳仿;
- **size的height必須大于(等于都不行!)stroke的width诡渴,否則,線無法顯示;**
- size的android:height屬性定義的是整個(gè)形狀區(qū)域的高度菲语。
###### 2.然后在自己的布局文件中添加一個(gè)LinearLayout作為不同view的分割線
<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="wrap_content"
android:showDividers="middle"
android:divider="@drawable/divider_dash"
android:background="@android:color/white"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="經(jīng)濟(jì)艙" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退改簽說明" />
</LinearLayout>
虛線畫好了妄辩,布局設(shè)置好了,遺憾的是山上,上述的方法和代碼也許在Android 3.0以下可以畫出虛線眼耀,但在Android 3.0以上最新Android SDK設(shè)備上,代碼在真機(jī)上跑起來佩憾,根本畫不出虛線哮伟,畫出的而是一條實(shí)線干花!
究其根本原因,是因?yàn)樵贏ndroid 3.0以上楞黄,Android系統(tǒng)在眾多繪圖操作時(shí)候默認(rèn)開啟了硬件加速池凄,因此導(dǎo)致在最新的高版本Android系統(tǒng)上畫dash gap line失效。解決方法主要有以下:
- 在Java代碼中給LinearLayout調(diào)用setLayerType(View.LAYER_TYPE_SOFTWARE, null)
- 在LinearLayout的xml布局文件設(shè)置屬性android:layerType="software"