2011.10.13(4)——— android android:layout_weight

参考: http://hi.baidu.com/hbzha/blog/item/8af2b44f9bd8bd1eb2de055b.html


1、
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    > <TextView         android:text="red"         android:background="#aa0000"         android:layout_width="wrap_content"         android:layout_height="100dip"/>     <TextView         android:text="green"         android:background="#00aa00"         android:layout_width="wrap_content"         android:layout_height="100dip"/>     <TextView         android:text="blue"         android:background="#0000aa"         android:layout_width="wrap_content"         android:layout_height="100dip"/>     <TextView         android:text="yellow"         android:background="#aaaa00"         android:layout_width="wrap_content"         android:layout_height="100dip"/></LinearLayout>


水平布局 textview的宽都是wrap_context

效果如下:





2、

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    > <TextView         android:text="red"         android:background="#aa0000"         android:layout_width="wrap_content"         android:layout_height="100dip"         android:layout_weight="1"/>     <TextView         android:text="green"         android:background="#00aa00"         android:layout_width="wrap_content"         android:layout_height="100dip"/>     <TextView         android:text="blue"         android:background="#0000aa"         android:layout_width="wrap_content"         android:layout_height="100dip"/>     <TextView         android:text="yellow"         android:background="#aaaa00"         android:layout_width="wrap_content"         android:layout_height="100dip"/></LinearLayout>


水平布局 textview的宽都是wrap_context 修改red的weight为1

效果如下:





通过1和2 可以得到结论:
在Horizontal的LinearLayout中,控件A和控件B的layout_weight分别设置为2和1,并不代表两者的宽度之比为2:1,2:1针对的是剩余的宽度。 控件的宽度等于空间本身需要的最小宽度,加上剩余宽度中的所占的权重。垂直方向的LinearLayout也同理。


3、

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    > <TextView         android:text="red"         android:background="#aa0000"         android:layout_width="wrap_content"         android:layout_height="100dip"         android:layout_weight="1"/>     <TextView         android:text="green"         android:background="#00aa00"         android:layout_width="wrap_content"         android:layout_height="100dip"         android:layout_weight="1"/>     <TextView         android:text="blue"         android:background="#0000aa"         android:layout_width="wrap_content"         android:layout_height="100dip"         android:layout_weight="1"/>     <TextView         android:text="yellow"         android:background="#aaaa00"         android:layout_width="wrap_content"         android:layout_height="100dip"         android:layout_weight="1"/></LinearLayout>


水平布局 textview的宽都是wrap_context 修改所有的textview的weight为1

效果如下:




可以看出来 textview并不是按照1:1:1:1的比例占据宽度的 所以正好验证了上面的结论

4、
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    > <TextView         android:text="red"         android:background="#aa0000"         android:layout_width="fill_parent"         android:layout_height="100dip"         android:layout_weight="1"/>     <TextView         android:text="green"         android:background="#00aa00"         android:layout_width="wrap_content"         android:layout_height="100dip"/>     <TextView         android:text="blue"         android:background="#0000aa"         android:layout_width="wrap_content"         android:layout_height="100dip"/>     <TextView         android:text="yellow"         android:background="#aaaa00"         android:layout_width="wrap_content"         android:layout_height="100dip"/></LinearLayout>


水平布局 red的宽为fill_parent 并且weight为1,其他textview的宽都是wrap_context

效果如下:




我们明明设置了red的weight为fill_parent 它却没有填充真个屏幕
所以通过上面的结果 我们可以得到一个信息:

当使用了layout_weight属性时,该属性优先于width和height属性。



5、

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    > <TextView         android:text="red"         android:background="#aa0000"         android:layout_width="fill_parent"         android:layout_height="100dip"         android:layout_weight="1"/>     <TextView         android:text="green"         android:background="#00aa00"         android:layout_width="wrap_content"         android:layout_height="100dip"         android:layout_weight="1"/>     <TextView         android:text="blue"         android:background="#0000aa"         android:layout_width="wrap_content"         android:layout_height="100dip"         android:layout_weight="1"/>     <TextView         android:text="yellow"         android:background="#aaaa00"         android:layout_width="wrap_content"         android:layout_height="100dip"         android:layout_weight="1"/></LinearLayout>


水平布局 red的宽为fill_parent 其他textview的宽都是wrap_context 修改所有的textview
的weight为1

效果如下:




这个不明白。。。。

更多相关文章

  1. android 错误
  2. Android(安卓)Log 机制
  3. Android(安卓)浅谈MatrixCursor
  4. android之button
  5. Android.Hacks.01_Centering views using weights
  6. Android使用DefaultHttpClient访问Wap网络
  7. android——xml drawable实现背景样式
  8. 查看 apk 的信息
  9. 四大布局 使用详解

随机推荐

  1. android studio3 多渠道打包及编译速度优
  2. Android(安卓)basic1
  3. Android平板获取唯一标识DeviceId
  4. Android(安卓)basic1
  5. Android(安卓)自带图标库 android.R.draw
  6. rock960 android box compiling!
  7. android对话框的使用
  8. Android(安卓)自带图标库 android.R.draw
  9. android 6 sdk/ndk下载地址
  10. android 跳转到应用通知设置界面(Android