app简单控件了解——视图——设置视图的宽和高
=====================================================================================
手机屏幕是长方形区域:短的叫宽,长的叫高。
控件宽度通过--------android:layout_width-----------设置
控件高度通过--------android:layout_height-----------设置
宽和高的取值,主要有3种:
(1)、match_parent:表示与上级视图保持一致。上级视图的尺寸有多大,当前视图的尺寸就有多大。
(2)、wrap_content:表示与内容自适应。对于文本视图来说,内部文字需要多大的显示空间,当前视图就要占据多大的尺寸。
但最宽不能超过上级视图的宽度,一旦超过,就会换行;最高不能超过上级视图的高度,一旦超过就会隐藏。
(3)、以dp为单位的具体尺寸,比如300dp,表示宽度和高度就这么大。
====================================================================================================
MainActivity:
package com.example.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void doClick(View view) { TextView tx = findViewById(R.id.textView); tx.setText("初始化成功-------------"); } }
xml配置:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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:clickable="true" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#F32152" android:baselineAligned="false" android:orientation="horizontal" tools:ignore="MissingConstraints"> <TextView android:id="@+id/textView" android:layout_width="80dp" android:layout_height="80dp" android:background="#2196F3" android:text="文字展示" android:textAlignment="center" android:translationX="0dp" android:translationY="0dp" /> <TextView android:id="@+id/textView2" android:layout_width="80dp" android:layout_height="80dp" android:background="#FFEB3B" android:text="国家展示" android:textStyle="bold" /> <Button android:id="@+id/button" android:layout_width="80dp" android:layout_height="80dp" android:background="#03A9F4" android:onClick="doClick" android:text="确定" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
------------------------------------------------------------------------------------------------------------------------
第一个text,宽度为80dp时,点击按钮,修改文字长度,
点击前:
点击后:
------------------------------------------------------------------------------------------------------------------------
第一个text,宽度为 android:layout_width="wrap_content" 时,点击按钮,修改文字长度,
点击前:
点击后:
==============================================================================
书本示例:
xml设置如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#00ffff" android:text="视图宽度采用wrap_content定义" android:textColor="#000000" android:textSize="17sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#00ffff" android:text="视图宽度采用match_parent定义" android:textColor="#000000" android:textSize="17sp" /> <TextView android:layout_width="300dp" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#00ffff" android:text="视图宽度采用固定大小" android:textColor="#000000" android:textSize="17sp" /> <TextView android:id="@+id/tv_code" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="#00ffff" android:text="通过代码指定视图宽度" android:textColor="#000000" android:textSize="17sp" /> </LinearLayout>
执行结果如下: