知識
不管是網站,軟件還是小程序,都要直接或間接能為您產生價值,我們在追求其視覺表現的同時,更側重于功能的便捷,營銷的便利,運營的高效,讓網站成為營銷工具,讓軟件能切實提升企業(yè)內部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
NewUI
發(fā)表時間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數:55
New UI-構造之LinearLayout(線性構造)詳解
——轉載請注明出處:coder-pig,迎接轉載,請勿用于貿易用處!
小豬Android開辟交換群已建立,迎接大年夜家參加,無論是新手,菜鳥,大年夜神都可以,小豬一小我的
力量畢竟是有限的,寫出來的器械肯定話芐很多忽略不足,迎接大年夜家指出,集思廣益,讓小豬的博文
加倍的詳盡,幫到更多的人,O(∩_∩)O感謝!
小豬Android開辟交換群:小豬Android開辟交換群群號:421858269
新Android UI實例大年夜全目次:http://blog.csdn.net/coder_pig/article/details/42145907
本節(jié)引言:
Android中的構造分為六大年夜構造,分別是:
LinearLayout(線性構造),RelativeLayout(相對構造),TableLayout(表格構造)
FrameLayout(幀構造),AbsoluteLayout(絕對構造),GridLayout(網格構造)
而今天我們要講解的就是第一個構造,LinearLayout(線性構造),我們屏幕適配的應用
用的比較多的就是LinearLayout的weight(權重屬性),在這一節(jié)里,我們會具體地解析
下LinearLayout,包含一些根本的屬性,Weight屬性的應用,以及比例若何計算,別的還
會說下一用的比較少的屬性:android:divider繪制下劃線
本節(jié)進修圖:
[img]http://img.blog.csdn.net/20150103132254125?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29kZXJfcGln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
2.weight(權重)屬性
①最簡蕩竽暌姑法:
如圖:
[img]http://img.blog.csdn.net/20150103132504359[img]http://img.blog.csdn.net/20150103132522460
實現代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:background="#ADFF2F" android:layout_weight="1"/> <LinearLayout android:layout_width="0dp" android:layout_height="fill_parent" android:background="#DA70D6" android:layout_weight="2"/> </LinearLayout>
要實現第一個的1:1的效不雅,只須要分別把兩個LinearLayout的weight改成1和1就可以了
用法歸納:
按比例劃分程度偏向:將涉及到的View的android:width屬性設置為0dp,然后設置為android
weight屬性設置比例即可;類推,豎直偏向,只需設android:height為0dp,然后設weight屬性即可!
大年夜家可以本身寫個豎直偏向的等比例劃分的體驗下簡蕩竽暌姑法!
②weight屬性詳解:
當然,如不雅我們不實用上述那種設置為0dp的方法,直接用wrap_content和match_parent的話,
則要接著解析weight屬性了,分為兩種情況,wrap_content與match_parent!別的還要看
LinearLayout的orientation是程度照樣豎直,這個決定哪個偏向等比例劃分
1)wrap_content比較簡單,直接就按比例的了
[img]http://img.blog.csdn.net/20150103133859699
實現代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="one" android:background="#98FB98" /> <TextView android:layout_weight="2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="two" android:background="#FFFF00" /> <TextView android:layout_weight="3" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="three" android:background="#FF00FF" /> </LinearLayout>
2)match_parent(fill_parent):這個責須要計算了
我們寫這段簡單的代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="one" android:background="#98FB98" /> <TextView android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="two" android:background="#FFFF00" /> <TextView android:layout_weight="3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="three" android:background="#FF00FF" /> </LinearLayout>
運行效不雅圖:
[img]http://img.blog.csdn.net/20150103134224309
這個時刻就會有疑問了,怎么會如許,這比例是2:1吧,那么three去哪了?代率攀瑯綾擎明明有
three的啊,還設置了3的,而1和2的比例也紕謬耶,1:2:3卻變成了2:1:0,怎么會如許呢?
答:這里其實沒那么簡單的,照樣須要我們計算的,網上給出的算法有幾種,這里就給出筆者
認為比較輕易懂得的一種:
step 1:個個都是fill_parent,然則屏幕只有一個啦,那么1 - 3 = - 2 fill_parent
step 2:依次比例是1/6,2/6,3/6
step 3:先到先得,先分給one,計算: 1 - 2 * (1/6) = 2/3 fill_parent
接著到two,計算: 1 - 2 * (2/6) = 1/3 fill_parent
最后到three,計算 1 - 2 * (3/6) = 0 fill_parent
step 4:所以最后的結不雅是:one占了兩份,two占了一份,three什么督杈有
以上就是為什么three沒有出現的原因了,或許大年夜家看完照樣有點蒙,沒事,我們舉多幾個
例子就知道了:
比例為:1:1:1
[img]http://img.blog.csdn.net/20150103134929859
按照膳綾擎的計算辦法算一次,結不雅是:1/3 1/3 1/3,沒錯
接著我們再試下:2:3:4
[img]http://img.blog.csdn.net/20150103135131464
計算結不雅:5/9 3/9 1/9,比較效不雅圖,5:3:1,也沒錯,所以這個計算辦法你可得mark下了!
③Java代碼設置weight屬性:
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
3.為LinearLayout設置瓜分線
很多界面開辟中都邑設置一些下劃線,或者瓜分線,大年夜而使得界面加倍整潔美不雅,比如下面的酷狗
音樂的注冊頁面:
[img]http://img.blog.csdn.net/20150103135703698
對于這種線,我們平日的做法有兩種
①直接在構造中添加一個view,這個view的感化僅僅是顯示出一條線,代碼也很簡單:
<View android:layout_width="match_parent" android:layout_height="1px" android:background="#000000" />這個是程度偏向上的黑線,當然你也可以改成其他色彩,或者應用圖片
[img]http://img.blog.csdn.net/20150103140121644
②第二種則是應用LinearLayout的一個divider屬性,直接為LinearLayout設置瓜分線
這里就須要你本身預備一張 線的圖片了
1)android:divider設置作為瓜分線的圖片
2)android:showDividers設置瓜分線的地位,none(無),begining(開端),end(停止),middle(每兩個組件間)
3)dividerPadding設置瓜分線的Padding
應用示例:
[img]http://img.blog.csdn.net/20150103142734205?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29kZXJfcGln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
實現代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@drawable/ktv_line_div" android:orientation="vertical" android:showDividers="middle" android:dividerPadding="10dp" tools:context="com.jay.example.linearlayoutdemo.MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3" /> </LinearLayout>
上述代碼中我們設置了瓜分線的圖片,以及設置瓜分線的出現地位,以及設置了padding,就實現了膳綾擎的效不雅!
當然我們也可以在代碼中setXxx或者getXxx設置或者獲得這些屬性的值
4.LinearLayout的簡單例子:
[img]http://img.blog.csdn.net/20150103143232805?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29kZXJfcGln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
實現代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請輸入要保存的德律風號碼" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> <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> </LinearLayout>
5.留意事項:
應用Layout_gravity的一個很重要的問題!!!
這個問題是一個讀者有時一次發(fā)明反饋給我的,萬分感激,同時欲望大年夜家在看小豬博客的時刻可以提出
一些實際開辟中碰到的問題,以及解決方法,好給后來者經驗!畢竟小豬不是神,不是什么方方面面都能
推敲到的,感謝!
問題內容:
在一個LinearLayout的程度偏向中安排兩個TextView,想讓一個左,一個右,怎么搞?
或許你會脫口而出:"gravity設置一個left,一個right就可以啦!"
真的┞封么簡單?你試過嗎?寫個簡單的Layout你就會發(fā)明,事與愿違了:
- <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="horizontal"
- tools:context="com.jay.example.getscreendemo.MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="200dp"
- android:layout_gravity="left"
- android:background="#FF7878"
- android:gravity="center"
- android:text="O(∩_∩)O哈哈~" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="200dp"
- android:layout_gravity="right"
- android:background="#FF7428"
- android:gravity="center"
- android:text="(*^__^*) 嘻嘻……" />
- </LinearLayout>
運行結不雅圖:
[img]http://img.blog.csdn.net/20141229160508444?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29kZXJfcGln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
看到這里你會說:哎呀,真的不可耶,要不在外層LinearLayout加個gravity=left的屬性,然后設置第二個
TextView的layout_gravity為right,恩,好我們試一下:
- <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="horizontal"
- android:gravity="left"
- tools:context="com.jay.example.getscreendemo.MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="200dp"
- android:background="#FF7878"
- android:gravity="center"
- android:text="O(∩_∩)O哈哈~" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="200dp"
- android:layout_gravity="right"
- android:background="#FF7428"
- android:gravity="center"
- android:text="(*^__^*) 嘻嘻……" />
- </LinearLayout>
結不雅照樣一樣:
[img]http://img.blog.csdn.net/20141229160508444?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29kZXJfcGln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
好吧,沒轍了,怎么搞妥?
當 android:orientation="vertical" 時, 只有程度偏向的設置才起感化,垂直偏向的設置不起感化。
即:left,right,center_horizontal 是生效的。
當 android:orientation="horizontal" 時, 只有垂直偏向的設置才起感化,程度偏向的設置不起感化。
即:top,bottom,center_vertical 是生效的。
不過貌似這個解決辦法有點坑爹,比如如不雅只能豎直偏向設置閣下對齊的話,就會出現下面的效不雅:
[img]http://img.blog.csdn.net/20140929110214095?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY29kZXJfcGln/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast
顯然不是我們要的結不雅把!
綜上,要么按照上述給出的規(guī)矩來構造,不過對于這種情況照樣應用相對構造RelativeLayout把!
網膳綾腔給出具體的原因,都是嗣魅如許改有人嗣魅這個和orientation的優(yōu)先級有關
,暫且先mark下來吧,后續(xù)如不雅知道原因的話再解釋!前面屏幕適配也說過了,構造照樣建議應用
RelativeLayout!