layout_weight详解

layout_weight在布局文件中经常使用,所有的视图都有一个layout_weight值,

默认为零,意思是需要显示多大的视图就占据多大的屏幕空间。若赋一个高于零的

值,则将父视图中的可用空间分割,分割大小具体取决于每一个视图layout_weight

 值。

如果第一个控件值为1,第二个控件值为2,则第一个控件占据本行的1/3,第

个占据2/3。这个比较好理解,就不举例子了。下面的例子说明只有一个控件值不

为0的时候,与layout_width的区别。

布局文件代码如下:

<?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="wrap_content"
    android:background="@color/light_blue"
    android:orientation="horizontal" >


    <ImageView
        android:id="@+id/headIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dip"
        android:layout_marginRight="4dip"
        android:layout_gravity="center_vertical"
        android:contentDescription="@string/app_name"
        android:src="@drawable/biz_navigation_tab_news_pressed" />

    <ImageView
        android:id="@+id/headDivider"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip"
        android:layout_gravity="center_vertical"
        android:contentDescription="@string/app_name"
        android:src="@drawable/base_action_bar_back_divider" />

    <TextView
        android:id="@+id/headTv"
        android:layout_width="wap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical"
        android:text="csdn资讯"
        android:layout_marginLeft="4dip"
        android:textSize="21sp"
        android:textStyle="bold"
        android:textColor="@color/white"
        />
</LinearLayout>

显示效果如下:


此时,TextView占据的大小即为包裹内容。

如果将TextView修改为:

    <TextView
        android:id="@+id/headTv"
        android:layout_width="0dip"
        android:layout_weight="1"

        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical"
        android:text="csdn资讯"
        android:layout_marginLeft="4dip"
        android:textSize="21sp"
        android:textStyle="bold"
        android:textColor="@color/white"
        />


textview就占据了剩下的所有空间。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值