布局TextView和EditText区别,layout_width和lay_weight区别--Android Studio

时间:2022-08-27 15:17:29
1. TextView控件是文本表示控件,主要功能是向用户展示文本的内容,它是不可编辑的,如设置标题;EditText控件是编辑文本控件,主要功能是让用户输入文本的内容,它是可以编辑的。
每一个控件都有着与之相应的属性,通过选择不同的属性,给予其值,能够实现不同的效果。
2. layout_width属性值设置为wrap_content时,获得的空间仅够描绘自身,还有额外的空间。layout_weight的属性值进行额外的空间分配。
如果layout_width="0dp",则只考虑layout_weight属性值。
<LinearLayout
    android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"> <Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/crime_date"
android:layout_weight="1"
/>
<CheckBox
android:id="@+id/crime_solved"
android:text="@string/crime_solved_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"/>
</LinearLayout>
如代码:LinearLayout有两个控件,Button 和CheckBox. 第一步查看layout_width属性值(竖直方向则查看layout_height)。因为设置为wrap_content时,获得的空间仅够描绘自身,还有额外的空间。
第二步LinearLayout依据layout_weight的属性值进行额外的空间分配。两个子组件属性值同为1。因为均分了同样大小的额外空间。若设Button的layout_weight的值为2,则将获得2/3的额外空间,
CheckBox组件获得剩余的1/3。