本节引言
本节开始讲Androidä¸çš„布局,Android䏿œ‰å…大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(è¡¨æ ¼å¸ƒå±€) FrameLayout(帧布局),AbsoluteLayout(ç»�对布局),GridLayout(ç½‘æ ¼å¸ƒå±€) 而今天我们è¦�讲解的就是第一个布局,LinearLayout(线性布局),我们å±�幕适é…�的使用 用的比较多的就是LinearLayoutçš„weight(æ�ƒé‡�属性),在这一节里,我们会详细地解æž� LinearLayout,包括一些基本的属性,Weight属性的使用,以å�Šæ¯”例如何计算,å�¦å¤–还 会说下一个用的比较少的属性:android:divider绘制下划线ï¼�
1.本节å¦ä¹ 图
2.weight(��)属性详解:
①最简�用法:
如图:
实现代ç �:
<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比较简�,直接就按比例的了
实现代ç �:
<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>
�行效果图:
这个时候就会有疑问了,æ€Žä¹ˆä¼šè¿™æ ·,这比例是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
按照上�的计算方法算一次,结果是:1/3 1/3 1/3,没错
接�我们�试下:2:3:4
计算结果:5/9 3/9 1/9,对比效果图,5:3:1,也没错,æ‰€ä»¥è¿™ä¸ªè®¡ç®—æ–¹æ³•ä½ å�¯å¾—mark下了!
â‘¢Java代ç �ä¸è®¾ç½®weight属性:
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1));
3.为LinearLayout设置分割线
很多界é�¢å¼€å�‘ä¸éƒ½ä¼šè®¾ç½®ä¸€äº›ä¸‹åˆ’线,或者分割线,从而使得界é�¢æ›´åŠ æ•´æ´�美观,比如下é�¢çš„é…·ç‹— 音ä¹�的注册页é�¢:
对于这ç§�线,我们通常的å�šæ³•æœ‰ä¸¤ç§� â‘ ç›´æŽ¥åœ¨å¸ƒå±€ä¸æ·»åŠ ä¸€ä¸ªview,这个view的作用仅仅是显示出一æ�¡çº¿,代ç �也很简å�•:
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000" />
这个是水平方å�‘上的黑线,å½“ç„¶ä½ ä¹Ÿå�¯ä»¥æ”¹æˆ�其他颜色,或者使用图片
②第二ç§�则是使用LinearLayout的一个divider属性,直接为LinearLayout设置分割线 这里就需è¦�ä½ è‡ªå·±å‡†å¤‡ä¸€å¼ çº¿çš„å›¾ç‰‡äº† 1)android:divider设置作为分割线的图片 2)android:showDividers设置分割线的ä½�ç½®,none(æ— ),beginning(开始),end(结æ�Ÿ),middle(æ¯�两个组件间) 3)dividerPadding设置分割线的Padding
使用示例:
实现代ç �:
<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>
4.LinearLayout的简å�•例å�:
实现代ç �如下:
<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的一个很é‡�è¦�的问题!!! 问题内容: 在一个LinearLayout的水平方å�‘ä¸å¸ƒç½®ä¸¤ä¸ªTextView,想让一个左,一个å�³,怎么æ�ž? æˆ–è®¸ä½ ä¼šè„±å�£è€Œå‡º:"gravity设置一个left,一个rightå°±å�¯ä»¥å•¦!" 真的这么简å�•?ä½ è¯•è¿‡å�—?写个简å�•çš„Layoutä½ å°±ä¼šå�‘现,事与愿è¿�了: 代ç �如下:
<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>
�行结果图:
çœ‹åˆ°è¿™é‡Œä½ ä¼šè¯´:哎呀,真的ä¸�行耶,è¦�ä¸�在外层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>
ç»“æžœè¿˜æ˜¯ä¸€æ ·ï¼š
好�,没辙了,怎么办好?
当 android:orientation="vertical" 时, �有水平方�的设置�起作用,垂直方�的设置�起作用。 �:left,right,center_horizontal 是生效的。 当 android:orientation="horizontal" 时, �有垂直方�的设置�起作用,水平方�的设置�起作用。 �:top,bottom,center_vertical 是生效的。
然而,这方法好�并没有什么�用。比如: 如果�能竖直方�设置左�对�的�,就会出现下�的效果:
这显然ä¸�是我们è¦�的结果把! 综上,è¦�么按照上述给出的规则æ�¥å¸ƒå±€,ä¸�过对于这ç§�情况还是使用相对布局RelativeLayout把ï¼� ç½‘ä¸Šæ²¡ç»™å‡ºå…·ä½“çš„åŽŸå› ,éƒ½æ˜¯è¯´è¿™æ ·æ”¹æœ‰äººè¯´è¿™ä¸ªå’Œorientation的优先级有关 ,暂且先mark下æ�¥å�§,å�Žç»å¦‚果知é�“åŽŸå› çš„è¯�å†�解释!å‰�é�¢å±�幕适é…�也说过了,布局还是建议使用 RelativeLayout!