布局LinearLayout代码

 

Xml代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >    
  7. <Button  
  8.       android:layout_width="fill_parent"  
  9.       android:layout_height="fill_parent"  
  10.       android:text="Button1"  
  11.       android:background="#008000"  
  12.       android:layout_weight="0"  
  13. />  
  14. <Button  
  15.       android:layout_width="fill_parent"  
  16.       android:layout_height="fill_parent"  
  17.       android:text="Button2"  
  18.       android:background="#FFFF00"  
  19.       android:layout_weight="0"  
  20. />  
  21. LinearLayout>  

 

layout_weight属性如果没有设置,则默认为0.

 

 

按钮1:button1,绿色

按钮2:button2,黄色

 

下面以按钮1和按钮2的weight属性(layout_weight)的变化,分别进行记录。

 

 

这里测试的是垂直布局android:orientation="vertical",所以针对组件的 android:layout_height属性;如果测试的水平布局,则需针对组件的 android:layout_width属性。

 

 

(一) android:layout_height = "fill_parent"

 

 

  button1(weight=0) button2(weight=0)   button1(weight=0) button2(weight=1)


 


 

button1(weight=1) button2(weight=1)

button1占1/2,button2占1/2

button1(weight=1) button2(weight=2)

button1占2/3,button2占1/3



 


 

button1(weight=1) button2(weight=9)

button1占9/10,button2占1/10

button1(weight=1) button2(weight=30)

button1占30/31,button2占1/31

   

 

 

(二) android:layout_height = "wrap_content"

 



 


 

button1(weight=0) button2(weight=0)

button1(weight=0) button2(weight=1)


 


 

button1(weight=1) button2(weight=1)

button1占1/2,button2占1/2

button1(weight=1) button2(weight=2)

button1占1/3,button2占2/3



 


 

button1(weight=1) button2(weight=9)

 

button1(weight=1) button2(weight=30)

 

   
   

 

通过以上实验可知:

(一) android:layout_height = "fill_parent"

(二) android:layout_height = "wrap_content"

在(一)、(二)2种情况在相同的weight属性下呈现出正好相反的情况。

 

 

更多相关文章

  1. ScrollView 的一些属性
  2. 《第一行代码》阅读笔记(三十四)——Material Design
  3. 如何修改三方构件的布局
  4. vlayout使用说明(二)
  5. Spinner的功能和用法
  6. MaterialDesign 布局/控件踩坑记
  7. Android(安卓)TextWatcher监控EditText中的输入内容并限制其输入
  8. 解决Viewpager满屏不能自适应填充内容的三种办法
  9. Android(安卓)触摸事件的传递过程

随机推荐

  1. Android TextView文字过多时通过滚动条显
  2. android逆向工程教程
  3. Android通过tcpdump抓包
  4. Android 实现截屏
  5. Android Json解析工具类
  6. Android庖丁解牛之WindowBackground
  7. Android多语言适配繁体中文
  8. 开始Android应用程序国内广告盈利模式
  9. Android中Log机制详解
  10. Android开发之旅:环境搭建及HelloWorld