android layout_weight="0dp"原因解析

本文探讨了Android中LinearLayout布局内元素如何使用layout_weight属性来分配空间。重点解释了android:layout_width=0dp与权重结合使用的正确方式,并通过实例展示了不同配置下视图的变化。

设置android:layout_weight="1“时,有时会发现android:layout_width="0dp",有时则发现android:layout_width="wrap_content"

其实正确的做法是android:layout_width="0dp",因为width的优先级高于weight。可参看如下链接文章

http://blog.youkuaiyun.com/u010552788/article/details/24736405


另外一盘文章:

http://blog.youkuaiyun.com/xiechengfa/article/details/38334327


 <?xml version="1.0" encoding="UTF-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="match_parent"   
    android:orientation="horizontal" >   
    <TextView   
        android:background="#ff0000"   
        android:layout_width="wrap_content"   
        android:layout_height="match_parent"   
        android:text="1"   
        android:textColor="@android:color/white"   
        android:layout_weight="1"/>                  #当width=wrap_content时,权值作用为1:2:3,权值小则图像窄
    <TextView   
        android:background="#cccccc"   
        android:layout_width="wrap_content"   
        android:layout_height="match_parent"   
        android:text="2"   
        android:textColor="@android:color/black"   
        android:layout_weight="2" />   
     <TextView   
        android:background="#ddaacc"   
        android:layout_width="wrap_content"   
        android:layout_height="match_parent"   
        android:text="3"   
        android:textColor="@android:color/black"   
        android:layout_weight="3" />   
</LinearLayout> 



 <?xml version="1.0" encoding="UTF-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="fill_parent"   
    android:layout_height="match_parent"   
    android:orientation="horizontal" >   
    <TextView   
        android:background="#ff0000"   
        android:layout_width="match_parent"   
        android:layout_height="match_parent"   
        android:text="1"   
        android:textColor="@android:color/white"   
        android:layout_weight="1"/>   #此时权值小则比较宽
    <TextView   
        android:background="#cccccc"   
        android:layout_width="match_parent"   
        android:layout_height="match_parent"   
        android:text="2"   
        android:textColor="@android:color/black"   
        android:layout_weight="2" />   
     
</LinearLayout> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ECECEC" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.4" android:orientation="horizontal"> <ImageView android:padding="2dp" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/img_jisuanqi"/> <TextView android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:text="计算器" android:textColor="#000000" android:textSize="15sp" android:layout_marginLeft="8dp" android:gravity="center_vertical"/> <ImageView android:layout_width="60dp" android:layout_height="30dp" android:src="@drawable/img_jianhao" android:padding="6dp"/> <ImageView android:layout_width="60dp" android:layout_height="30dp" android:padding="6dp" android:src="@drawable/img_square"/> <ImageView android:layout_width="60dp" android:layout_height="30dp" android:padding="6dp" android:src="@drawable/img_cha"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.6" android:orientation="horizontal" android:layout_marginTop="10dp" android:gravity="center"> <ImageView android:padding="6dp" android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/img_gang"/> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="标准" android:textColor="#000000" android:textSize="30sp" android:textStyle="bold" android:layout_marginLeft="8dp" android:gravity="center_vertical"/> <ImageView android:layout_width="0dp" android:layout_height="30dp" android:layout_weight="1" android:scaleType="fitStart" android:src="@drawable/img_top"/> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:padding="6dp" android:src="@drawable/img_lishi"/> </LinearLayout>解析
10-21
<com.mediatek.keyguard.Telephony.KeyguardSimPinPukMeView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:androidprv="http://schemas.android.com/apk/res-auto" android:id="@+id/keyguard_sim_pin_puk_me_view" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" androidprv:layout_maxWidth="@dimen/keyguard_security_width" androidprv:layout_maxHeight="@dimen/mtk_keyguard_security_max_height" android:gravity="center_horizontal"> <LinearLayout android:id="@+id/sim_info_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:gravity="center_vertical"> <TextView android:id="@+id/slot_num_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:ellipsize="marquee" android:singleLine="true" android:textDirection = "locale" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="?attr/wallpaperTextColor" android:visibility="gone" /> <ImageView android:id="@+id/keyguard_sim" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_lockscreen_sim"/> </LinearLayout> <LinearLayout android:id="@+id/keyguard_bouncer_frame" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical" android:layout_weight="1" android:layoutDirection="ltr" > <RelativeLayout android:id="@+id/row0" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:paddingBottom="16dp" > <com.android.keyguard.PasswordTextView android:id="@+id/simPinPukMeEntry" android:layout_width="@dimen/keyguard_security_width" android:layout_height="match_parent" android:gravity="center" android:layout_centerHorizontal="true" android:layout_marginRight="72dp" androidprv:scaledTextSize="28" /> <com.android.keyguard.NumPadButton android:id="@+id/delete_button" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:clickable="true" android:paddingTop="8dip" android:paddingBottom="8dip" android:paddingRight="8dp" android:paddingLeft="24dp" android:contentDescription="@string/keyboardview_keycode_delete" android:layout_alignEnd="@+id/simPinPukMeEntry" android:layout_alignParentRight="true" style="@style/NumPadKey.Delete" /> <View android:id="@+id/divider" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="#28FFFFFF" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <com.android.keyguard.NumPadKey android:id="@+id/key1" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="1" /> <com.android.keyguard.NumPadKey android:id="@+id/key2" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="2" /> <com.android.keyguard.NumPadKey android:id="@+id/key3" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <com.android.keyguard.NumPadKey android:id="@+id/key4" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="4" /> <com.android.keyguard.NumPadKey android:id="@+id/key5" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="5" /> <com.android.keyguard.NumPadKey android:id="@+id/key6" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1" > <com.android.keyguard.NumPadKey android:id="@+id/key7" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="7" /> <com.android.keyguard.NumPadKey android:id="@+id/key8" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="8" /> <com.android.keyguard.NumPadKey android:id="@+id/key9" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="9" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/key_dismiss" style="@style/DismissPadKey" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" android:visibility="invisible" android:text="DISMISS" /> <com.android.keyguard.NumPadKey android:id="@+id/key0" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="0" /> <com.android.keyguard.NumPadButton android:id="@+id/key_enter" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" style="@style/NumPadKey.Enter" android:contentDescription="@string/keyboardview_keycode_enter" /> </LinearLayout> </LinearLayout> <include layout="@layout/keyguard_eca" android:id="@+id/keyguard_selector_fade_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="bottom|center_horizontal" android:gravity="center_horizontal"/> </com.mediatek.keyguard.Telephony.KeyguardSimPinPukMeView> 该界面数字键显示不全,分析问题并修改布局
最新发布
11-28
<? xml version = "1.0"encoding = " utf-8" ? > android : layout_width = " match_parent" <LinearLayout xmlns: android = " http://schemas.android.com/apk/res/android" android :layout_height = " match_parent" android: orientation = " vertical" ><HorizontalScrollView android: layout_width = " wrap_content" android:layout_height = " 0dp" android:layout_weight ="1" android:scrollbars = " none" >< LinearLayout android:layout_width = " wrap_content" android:layout_height = " match_parent" android :orientation = " horizontal" ><View android: layout_width = "300dp" android :layout_height = " match_parent" android:background = "@color/red"/ ><View android:layout_width = "300dp" android ;layout_height = " match_parent" android:background = " @color/green"/ ><View android :layout_width ="300dp" android :layout_height = " match_parent" android:background = " @color/blue"/></LinearLayout ></HorizontalScrollView ><ScrollView android :layout_width = " match_parent"android :layout_height ="0dp" android :layout_weight ="1" android : scrollbars = " none" ><LinearLayout android :layout_width = " match_parent" android :layout_height = " wrap_content" android :orientation =" vertical" ><View android :layout_width = " match_parent" android :layout_height ="200dp" android:background =" @color/yellow"/ ><View android:layout_width = " match_parent" android :layout_height ="200dp" android;background =" @color/cyan"/ ><View android :layout_width = " match_parent" android:layout_height ="200dp" android :background = " @color/blue"/> </LinearLayout ></ScrollView>/LinearLayout >修正补全
09-18
给你一个布局参考: <?xml version="1.0" encoding="utf-8"?> <!-- 标题部分 --> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="更新成功" android:textSize="24sp" /> <!-- 版本信息部分 --> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.5" android:orientation="vertical" android:gravity="center_horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="目标更新版本:202506" android:textSize="18sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当前版本:202506" android:textSize="18sp" /> </LinearLayout> <!-- 对勾图标部分 --> <ImageView android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1.5" android:layout_gravity="center" android:src="@drawable/ic_check" /> <!-- 这里需要替换为实际的对勾图标资源 --> <!-- 提示文字部分 --> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="恭喜您,数据更新成功" android:textSize="18sp" /> <!-- 按钮部分 --> <Button android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:layout_gravity="center" android:text="我知道了" /> </LinearLayout> 详细的说清楚整个布局的像素框位置和内容
09-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值