android布局LinearLayout的使用

序:本文讲述Android布局中的LinearLayout的使用,重点2方面:

1. 跨行和跨列如何实现 

2.使用layout_weight注意事项

-------------------------------------------------------------------------------------------------------------------------------------------

1. 跨列如何实现?(例如一行 有2个按钮,其中左边一个按钮长度是右边按钮的2倍,如下图的按钮0)。

实现方法:a. 先设置 按钮0  的layout_weight = "2" , layout_width=“0dp”

                  b. 再设置 按钮·   的layout_weight = "1" , layout_width=“0dp” 即可。


2. 使用layout_weight注意事项

       在设置layout_weight的view(比如Button) 或Component(比如LinearLayout)设置layout_weight值时,要将对应的view(比如Button) 或Component(比如LinearLayout)的layout_width设置为“0dp” ,这样系统能自动按比例分配空间。因为之前的文章也讲了,layout_weight的权重,是将系统默认分配后剩余的空间(因为系统刚开始会根据layout_width的值先自动分配空间)按比例分配。所以不写layout_width="match_parent",而写成 layout_width=“0dp”.这样是说,系统刚开始不自动分配空间,所以剩余的空间就是初始空间,就能按比例分配了。

简单概括:一行横向有2个按钮:button1,button2.  使得button1的宽 : button2宽的 = 2:1 的方法如下

button1 : android:layout_weight = "2" , android:layout_width="0dp"

button2 : android:layout_weight = "1" , android:layout_width="0dp" 


同理:

对于component(比如LinearLayout)设置比例宽度方法一致,以下图举例。

如上图,左边LinearLayout宽 : 右边LinearLayout宽 = 3 : 1 。实现方法如下:

将一个大的LinearLayout 分成 左边一个LinearLayout 和 右边一个LinearLayout ,宽度比为 3:1 。关键代码如下:

强调1.左边宽3倍设置方法:    android:layout_width="0dp" android:layout_weight="3"

强调2.右边宽1倍设置方法:    android:layout_width="0dp" android:layout_weight="1"


     <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"  >  
        
             <LinearLayout 
	        android:orientation="vertical"
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="3"  > 
	        
                            省略中间具体内容
            
             
	     </LinearLayout><!-- 对应两行整体左边 2/3 -->  
	     
	     <LinearLayout         
	        android:layout_width="0dp"
	        android:layout_height="match_parent"
	        android:layout_weight="1"  >
             
	                       省略中间具体内容	         
	         
	     </LinearLayout><!-- 对应两行整体右边 1/3 --> 
     </LinearLayout><!-- 总体linearlayout --> 


-------------------------------------------------------------------------------------------------------------------------------------------

实际程序结果图:

页面代码:/res/layout/activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical" >
	<EditText
		android:layout_width="match_parent"
		android:layout_height="wrap_content" >
	</EditText>
	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:orientation="horizontal" >
             "
		<Button
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="1"
			android:text="+" />
		<Button
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="1"
			android:text="-" />
		<Button
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="1"
			android:text="*" />
		<Button
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="1"
			android:text="/" />
	</LinearLayout>
	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:orientation="horizontal" >
		<LinearLayout
			android:layout_width="0dp"
			android:layout_height="wrap_content"
			android:layout_weight="3"
			android:orientation="vertical" >
			<LinearLayout
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:orientation="horizontal" >
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:text="1" />
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:text="2" />
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:text="3" />
			</LinearLayout> <!-- 对应左边2/3 的上一行 -->
 
			<LinearLayout
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:orientation="horizontal" >
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="2"
					android:text="0" />
				<Button
					android:layout_width="0dp"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:text="." />
			</LinearLayout><!-- 对应左边2/3 的下一行 -->

 		</LinearLayout> <!-- 对应下两行整体左边 2/3 -->
 
		<LinearLayout
			android:layout_width="0dp"
			android:layout_height="match_parent"
			android:layout_weight="1" >
			<Button
				android:layout_width="match_parent"
				android:layout_height="match_parent"
				android:text="=" >
			</Button>
		</LinearLayout> <!-- 对应下两行整体右边 1/3 -->
 
	</LinearLayout><!-- 对应下面两大行的总体linearlayout -->
 
</LinearLayout>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值