Android中的android:layout_width和android:width

Android布局详解
本文详细解析了Android中的布局属性,特别是android:width与android:layout_width的区别,并通过实例演示了它们如何影响视图组件的显示效果。

android:width 其实是定义控件上面的文本(TextView) 的宽度,当然这个宽度也是和 android:layout_width 配合起来作用的,如果 android:layout_width="fill_parent" 的话,那么设置 android:width 是没有意义的


android:layout_width 其实是可以实现 android:width 的效果的,我觉得这应该是为什么在 android 实例中看不到有人用 android:width 的原因吧。


若还要讲讲两者的区别的话,那就是:
android:width 的值,一般是 "100dp" 这样的数值;
android:layout_width 的值,一般是"fill_parent","wrap_content","match_parent".当然,它也可以像前者一样,设置数值的.


带"layout"的属性是指整个控件而言的,是与父控件之间的关系,如 layout_gravity 在父控件中的对齐方式, layout_margin 是级别相同的控件之间的间隙等等;

不带"layout" 的属性是指控件中文本的格式,如gravity是指文本的对齐方式等等,而其中文本的格式又受制约于它的控件在父控件中的属性.


借用一位大牛的示例:http://zhangcong170.iteye.com/blog/423173

<?xml version="1.0" encoding="utf-8"?>  
<!--   
	<LinearLayout>  
	线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的  
 -->  
<LinearLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"   
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
   <!-- android:orientation="vertical" 表示竖直方式对齐  
        android:orientation="horizontal"表示水平方式对齐  
        android:layout_width="fill_parent"定义当前视图在屏幕上 可以消费的宽度,fill_parent即填充整个屏幕。  
        android:layout_height="wrap_content":随着文字栏位的不同 而改变这个视图的宽度或者高度。有点自动设置框度或者高度的意思  
                
		layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。  
		
		所有的视图都有一个layout_weight值,默认为零,意思是需要显示  
		多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视  
		图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight  
		值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布  
		局的layout_weight值中所占的比率而定。  
		
		举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。  
		该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。  
		如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分  
		在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个   
		文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,  
		则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要  
		度越高)。  
    -->  
    <LinearLayout  
    android:orientation="horizontal"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:layout_weight="1">  
    <TextView  
        android:text="red"  
        android:gravity="center_horizontal"  
        android:background="#aa0000"  
        android:layout_width="wrap_content"  
        android:layout_height="fill_parent"  
        android:layout_weight="1"/>  
      
    <TextView  
        android:text="green"  
        android:gravity="center_horizontal"  
        android:background="#00aa00"  
        android:layout_width="wrap_content"  
        android:layout_height="fill_parent"  
        android:layout_weight="1"/>  
      
    <TextView  
        android:text="blue"  
        android:gravity="center_horizontal"  
        android:background="#0000aa"  
        android:layout_width="wrap_content"  
        android:layout_height="fill_parent"  
        android:layout_weight="1"/>  
      
    <TextView  
        android:text="yellow"  
        android:gravity="center_horizontal"  
        android:background="#aaaa00"  
        android:layout_width="wrap_content"  
        android:layout_height="fill_parent"  
        android:layout_weight="1"/>  
          
    </LinearLayout>  
      
    <LinearLayout  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:layout_weight="2">  
      
    <TextView  
        android:text="row one"  
        android:textSize="15pt"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_weight="1"/>  
      
    <TextView  
        android:text="row two"  
        android:textSize="15pt"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_weight="1"/>  
      
    <TextView  
        android:text="row three"  
        android:textSize="15pt"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_weight="1"/>  
      
    <TextView  
        android:text="row four"  
        android:textSize="15pt"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_weight="1"/>  
          
    </LinearLayout>  
          
</LinearLayout>  


Android 布局中,若希望子视图的高度与其父视图的高度一致,通常使用 `android:layout_height="match_parent"` 来实现。该属性表示当前视图将扩展其高度以匹配父容器的可用高度,是 Android 布局系统中控制尺寸的标准方式之一[^1]。 例如,在一个 `RelativeLayout` 或 `LinearLayout` 中,若希望某个子视图(如 `ImageView` 或 `LinearLayout`)占据整个父容器的高度,可以如下设置: ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="400dp" android:background="#008000"> <ImageView android:src="@drawable/ic_launcher" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> ``` 上述代码中,`ImageView` 的 `layout_height` 设置为 `match_parent`,使其高度与父容器 `RelativeLayout` 保持一致[^2]。 在某些嵌套布局场景中,例如 `ScrollView` 内部的子视图设置 `match_parent` 时,可能会出现高度不生效的问题。此时需要确保 `ScrollView` 设置了 `android:fillViewport="true"` 属性,以允许内部视图扩展到整个可用高度: ```xml <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 子视图内容 --> </LinearLayout> </ScrollView> ``` 此外,若在 `RecyclerView` 或 `ListView` 中设置 `item` 的高度为 `match_parent`,需确保在 `onCreateViewHolder` 方法中正确传入 `parent` 参数,并设置 `false` 作为第三个参数,以确保布局参数正确应用: ```java @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false); return new ViewHolder(view); } ``` 需要注意的是,`android:layout_height` 是用于布局管理器中,表示视图如何根据父容器调整大小,而 `android:height` 是直接设置视图的具体高度值(以像素为单位),通常不推荐使用该属性来控制布局尺寸[^1]。 ### 相关问题 1. `android:layout_height="wrap_content"` 为何不起作用? 2. 如何在 `RecyclerView` 中正确设置 `item` 的宽度为 `match_parent`? 3. `android:width` 与 `android:layout_width` 的具体区别是什么?
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值