Android控件学习(一)——TextView

TextviewAndroid最简单的一个控件了,主要用于再界面上显示一段文本信息。

一、代码示例

先新建个空项目,名字叫UIWidget
然后修改主布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This is TextView"
        />

</LinearLayout>

关于里面各个参数是啥意思,可以参考这篇:《安卓四大组件之活动》

二、运行实例

TextView中的文字默认是左上角对齐的
在这里插入图片描述
我们修改布局,使用android:gravity控制文字的对齐方式:

<TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="This is TextView"
        />

center = center_vertical + center_horizontal
在这里插入图片描述
还可以对文字的大小和颜色修改:

<TextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="24sp"
    android:textColor="#00ff00"
    android:text="This is TextView"
    />

在这里插入图片描述

发布了156 篇原创文章 · 获赞 13 · 访问量 7249

猜你喜欢

转载自blog.csdn.net/qq_41205771/article/details/103896444