Android开发——线性布局和TextView基本用法

Android开发——线性布局和TextView的基本用法

一、线性布局-LinearLayout

在LinearLayout这个控件中,有这许多属性可以设置,下面是最为基础的属性

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:gravity="center">
</LinearLayout>

1、其中的android:layout_width指的是控件的宽度
android:layout_height指的是控件的高度
这两个属性后面的参数可以写计数单位来确认,也可以使用wrap_content和match_parent,他们分别表示的是最小值和最大值
2、android:orientation是指的是布局的方向,在线性布局中,方向分为横向和竖向
其中vertical表示为竖向排布
horizontal表示为横向排布
3、android:gravity属性是控件内的布局
center为居中
top为最顶端
bottom为最低端
left为最左端
right为最右端
如果想让两个属性同时存在,在中间打上||即可

二、文本控件-TextView

在TextView这个控件中也有许多属性需要我们进行设置

<TextView
            android:id="@+id/Hello"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World"
            android:textColor="#000000"
            android:textStyle=""
            android:textSize="16dp">
</TextView>

1、其中android:layout_width和android:layout_height的设置方法和LinearLayout的一致
2、android:text是设置TextView控件中显示文字的属性
3、android:textColor是设置文本颜色,颜色代码是16进制的
4、android:textStyle是字体的形式,bold是粗体,
italic是斜体
5、android:textSize是字体大小,单位是dp,px,sp等

三、使用Java改变控件属性

在日常的开发中,控件需要不停的变换,给用户带来更多更简便的操作。因为xml文件中的属性是死的,这就需要使用Java来改变属性。
操作步骤如下:
1、先给需要控制的控件赋予一个ID
2、找到该控件所在xml文件对应的Java文件
3、在Java文件中添加控制控件的代码

//获取控件
TextView textview=(TextView)super.findViewById(R.id.Hello);
//设置属性
textview.setText("Change Text");

提示:一般情况下,set开头的为写入事件,get为获取

猜你喜欢

转载自blog.csdn.net/zyf918/article/details/85086153