android:layout_width与android:width的区别

最近正在自学安卓开发,先是学习界面编程,在学习当中看到书上TextView控件有android:layout_width属性与android:width属性,一下搞不明白,所以写了个Demo测试,以下是xml文件
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
     android:id="@+id/view1"
     android:layout_width="40dp"
     android:layout_height="wrap_content"
     android:background="#0f0"
     android:width="300dp"
     android:text="helloworld"
        />


    <TextView
    android:id="@+id/view2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#f00"
    android:width="300dp"
    android:text="helloworld"
    />

    <TextView
        android:id="@+id/view3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00f"
        android:width="30dp"
        android:text="helloworld"
        />


    <TextView
        android:id="@+id/view4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#90f"
        android:width="30dp"
        android:text="helloworld"
        />

    <TextView
        android:id="@+id/view5"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:background="#28f"
        android:width="30dp"
        android:text="helloworld"
        />
</LinearLayout>

运行结果如下所示:


根据以上运行结果得出以下结论:
1.layout_width主要是相对父控件而言设置的宽度,可以为wrap_content,fill_parent以及具体的宽度。而width是对于自身而言设置的宽度,一般赋值都是具体的宽度。
2.当layout_width为wrap_content时,控件真实的宽度由width决定;当layout_width为fill_parent或具体某个宽度时,控件真实宽度由layout_width决定

本人是初学者,如有错误,敬请指正,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值