Android应用开发 文本显示

一、设置文本内容

1.在xml文件中通过属性android:text设置文本

    <TextView
    android:id="@+id/tv_hello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="你好,世界" />

2.通过java代码调用文本视图对象的setText方法设置文本

TextView tv_hello = findViewById(R.id.tv_hello);
tv_hello.setText("你好,世界"); // 设置tv_hello的文字内容

但是,android studio不推荐在xml布局文件里直接写字符串,推荐放在一个名为strings.xml的地方统一管理,方便复用。

<resources>
    <string name="app_name">chapter03</string>
    <!-- 添加 -->
    <string name="hello">你好,世界</string>
</resources>

添加完后,在xml布局文件中,可以讲text属性值改为@string/字符串名

    <TextView
        android:id="@+id/tv_hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

java代码中引用字符串资源,需要使用R.string.字符串名,java可以使用

//tv_hello.setText(R.string.hello); // 设置tv_hello的文字资源

二、设置文本大小

java代码中通过setTextSize方法即可指定文本大小

tv_sp.setTextSize(30); // 设置tv_sp的文本大小

在xml文件中通过属性android:textSize指定文本大小,但是xml文件中设置时需要带上字号单位,常见的字号单位主要有px,dp,sp三种:

1.px(pixel)  在Android中不常用,因为一张100px*100px的图片在相同屏幕尺寸但分辨率不同的设备上显示的大小是不一致的。通俗的讲就是在相同屏幕尺寸下,相同大小的图片在越大的分辨率下显示的越小。

2.dp(Density independent Pixels)  这是一种基于设备密度的抽象单位,它取决于屏幕的密度,以确保在不同密度的设备上显示相同的视觉尺寸。1dp大约等于屏幕上的1个像素(在中密度屏幕上)。使用dp作为文本大小单位可以确保在不同屏幕密度的设备上具有一致的大小,使得应用程序更具可移植性和适配性。

3.sp(Scale independent Pixel) sp的原理跟dp差不多,但它专门用来设置字体大小,也是Android推荐的字号单位。手机在系统设置里可以调整字体的大小(小、标准、大、超大)。设置普通字体时,同数值dp和sp的文字看起来一样大;如果设置为大字体,用dp设置的文字没有变化,用sp设置的文字就变大了。

三、设置文本颜色

在Java代码中调用setTextColor方法即可设置文本颜色,具体在Color类中定义了12种颜色

TextView tv_code_system = findViewById(R.id.tv_code_system);
// 将tv_code_system的文字颜色设置系统自带的绿色
tv_code_system.setTextColor(Color.GREEN);

但是xml文件无法引用Color类的颜色常量,为此Android制定了一套规范的编码标准,将色值交由透明度alpha和RGB三原色联合定义。

该标准有8位十六进制和6位十六进制两种表达形式,例如8位编码FFEEDDCC中,FF表示透明度,EE表示红色,DD表示绿色,CC表示蓝色浓度。

6位十六位编码默认不透明。

        // 将tv_code_six的文字颜色设置为透明的绿色,透明就是看不到
        tv_code_six.setTextColor(0x00ff00);

        // 将tv_code_eight的文字颜色设置为不透明的绿色,即正常的绿色
        tv_code_eight.setTextColor(0xff00ff00);

xml中可以通过android:textColor设置文本颜色,但要给色值添加井号前缀(#),例如

    <TextView
        android:id="@+id/tv_xml"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="布局文件设置六位文字颜色"
        android:textColor="#00ff00"
        android:textSize="17sp" />

就像字符串一样,Android把颜色也作为一种资源,在res/values目录下的colors.xml,发现里面已经定义了3种颜色:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
</resources>

因此,我们也可以在这个resources节点补充颜色的常量定义:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>

    <color name="black">#000000</color>
    <color name="white">#ffffff</color>
    <color name="green">#00ff00</color>
</resources>

后续我们就可以通过@color/颜色名称,也就是来引用我们定义的颜色

    <TextView
        android:id="@+id/tv_values"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="资源文件引用六位文字颜色"
        android:textColor="@color/green"
        android:textSize="17sp" />

不仅文本颜色,背景颜色同样可以使用上述资源,在xml文件中通过属性android:background设置控件的背景颜色,java代码有两种方式设置背景颜色,如果色值来自Color类或者(8位/6位)十六进制数,则调用setBackgroundColor方法设置背景颜色,如果设置来自colors.xml中的颜色资源,则调用setBackgroundResource方法,以R.color.颜色名称的格式设置背景颜色。例如

        tv_code_background.setBackgroundColor(Color.GREEN); // 在代码中定义的色值
        tv_code_background.setBackgroundResource(R.color.green); // 颜色来自资源文件

属性android:background和setBackgroundResource不仅可以用来设置背景颜色,还可以用来设置背景图片,资源访问方式为R.drawable.不含扩展名的图片名称。

  • 11
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值