详细请点击:http://www.verydemo.com/demo_c131_i118272.html

....................................................................

如果在一个LinearLayout里面放置两个Button,Button1和Button2,Button1的layout_weight设置为1,Button2的layout_weight设置为2,且两个Button的layout_width都设置为fill_parent。

<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2"/>
</LinearLayout>

则Button1占据屏幕宽度的三分之二,而Button2占据三分之一,如下图所示:

如果两个Button的layout_width都设置成wrap_content,则情况刚好相反。Button1占三分之一,Button2占三分之二,如下图所示:

layout_weight在使用LinearLayout设计复杂的布局时还是挺有用处的,例如,在水平的线性布局中,你要分足够的空间给控件1,剩下的空间则分配给控件2,则只要设置控件1的layout_width设置为wrap_content,不用设置layout_weight,而在控件2中,设置layout_width为fill_parent,layout_weight为1即可实现。

更多相关文章

  1. android之相对布局示例
  2. Android RelativeLayout 相对布局
  3. 介绍两个Android开源项目:Android显示GIF动画
  4. 日期选择控件
  5. Android创建菜单栏(android布局实现)
  6. 【Android随记】-- Android动态加载布局
  7. 控件 -- WebView -- Android与JS交互
  8. Android--取得布局中指定控件的宽高
  9. android计算器布局(TableLayout)

随机推荐

  1. Android(安卓)EditText的setOnEditorActi
  2. Kivy A to Z -- Android(安卓)apk的生成
  3. mac下Android(安卓)studio gradle 配置
  4. android通过MediaProjectionManager录屏
  5. 深入理解 Android(安卓)中的各种 Context
  6. 用jquery,js 实现h5,web端跳转app,安卓&ios
  7. android之自定义组合控件
  8. Android(安卓)getCacheDir和getFilesDir
  9. Android(安卓)的OkHttp(发送网络请求)
  10. 查看系统属性build.prop