Layout_weight相关

2015-11-18  本文已影响60人  EvilsoulM

Demo 1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent"    
  android:layout_height="match_parent"   
  android:orientation="horizontal">    
<TextView        
  android:layout_width="0dp"        
  android:layout_height="40dp"        
  android:layout_weight="1"        
  android:background="@color/black"        
  android:gravity="center"        
  android:text="weight 123" />    
<TextView        
  android:layout_width="0dp"        
  android:layout_height="40dp"        
  android:layout_weight="2"        
  android:background="@color/red_press"        
  android:gravity="center"        
  android:text="hahaha" />    
<TextView        
  android:layout_width="0dp"        
  android:layout_height="40dp"        
  android:layout_weight="3"              
  android:background="@color/main_blue"        
  android:gravity="center"        
  android:text="hahaha" />
</LinearLayout> 

效果

Paste_Image.png

比例都显示的对的,为什么第一行的布局会向下移动呢?
其实对于控件不是对齐的,但是对于文字第一行是对齐的,textview在linearlayout中回去参考父类的基线(baselineAligned)只要将linearlayout的baselineAligned设置为false就可以了

Demo 2

将width改成wrap_content会变成什么样子呢?按照weight的作用应该还是1:2:3

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent"    
  android:layout_height="match_parent"   
  android:orientation="horizontal">    
<TextView        
  android:layout_width="wrap_content"        
  android:layout_height="40dp"        
  android:layout_weight="1"        
  android:background="@color/black"        
  android:gravity="center"        
  android:text="weight 123" />    
<TextView        
  android:layout_width="wrap_content"        
  android:layout_height="40dp"        
  android:layout_weight="2"        
  android:background="@color/red_press"        
  android:gravity="center"        
  android:text="hahaha" />    
<TextView        
  android:layout_width="wrap_content"        
  android:layout_height="40dp"        
  android:layout_weight="3"              
  android:background="@color/main_blue"        
  android:gravity="center"        
  android:text="hahaha" />
</LinearLayout> 
Paste_Image.png

和我们预期的1比2比3的布局完全不同,其实在这里linearlayout会优先使用我们声明的值进行分配,然后再将剩下的尺寸按照weight进行分配

Demo 3

我们再将宽度改为match_parent

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent"    
  android:layout_height="match_parent"   
  android:orientation="horizontal">    
<TextView        
  android:layout_width="match_parent"        
  android:layout_height="40dp"        
  android:layout_weight="1"        
  android:background="@color/black"        
  android:gravity="center"        
  android:text="weight 123" />    
<TextView        
  android:layout_width="match_parent"        
  android:layout_height="40dp"        
  android:layout_weight="2"        
  android:background="@color/red_press"        
  android:gravity="center"        
  android:text="hahaha" />    
<TextView        
  android:layout_width="match_parent"        
  android:layout_height="40dp"        
  android:layout_weight="3"              
  android:background="@color/main_blue"        
  android:gravity="center"        
  android:text="hahaha" />
</LinearLayout> 
Paste_Image.png

会发现第三个textview直接看不到了,那么我们用上面的结论来验证一下
假设我们的屏幕宽度640
如果我们是matchparent也就是说三个textview各会分配640 的宽度
那么剩下的宽度为 640 -3*640=-1280
然后我们再按照比例进行分配
textview 1 : -1280 / 6 *1=-213
textview 2 : -1280 /6 *2=-427
textview 3: -1280 /6 *3 = -640

那么每个textview的实际展示宽度也不难算出
textview1:640+(-213)=427
textview2:640+(-427)=213
textview3:640+(-640)=0

和我们的展示效果也就相符了

weightsun也是weight搭配使用的标签

上一篇下一篇

猜你喜欢

热点阅读