android:layout_width与android:width的区别

来源:互联网 发布:我的阿里云tv工程模式 编辑:程序博客网 时间:2024/04/29 17:02
最近正在自学安卓开发,先是学习界面编程,在学习当中看到书上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决定

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

1 0
原创粉丝点击