style 来实现 TextView 的字体格式定制

文章目录


####1、功能介绍
当我们在使用TextView 的时候 ,通常需要设置 统一的字体颜色 ,大小 ,透明度、等属性。
为了减少工作量,避免在每一个TextView 里面都设置这些属性,我们可以写一个 Style.xml 文件,自定义一些 Text View的一些属性 格式。
然后在 用到的 Text View里 调用

####2、代码架构

这里写图片描述

####3、style 文件
style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="textStyle_01">  <!--字体一 样式-->
        <item name="android:textSize"> 28dp</item>  <!--设置大小-->
        <item name="android:color">#FFFFFFFF</item>   <!--设置颜色-->
    </style>

    <style name="tetStyle_02">    <!--字体二 样式-->
        <item name="android:textSize">35dp</item>  <!--设置大小-->
        <item name="android:color">#FF4081</item>  <!--设置颜色-->
        <item name="android:alpha">0.5</item>   <!--设置透明度-->
    </style>
</resources>

####4、TextView 组件 对字体样式的引用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.lum.textstyle.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="15dp"
        android:textColor="@color/colorPrimaryDark"/>

    <TextView
        style="@style/textStyle_01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <TextView
        style="@style/tetStyle_02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

</LinearLayout>

效果:
这里写图片描述

要在 Android 的 TextView实现字体颜色渐变的效果,你可以使用 SpannableString 和 ForegroundColorSpan 组合来实现。下面是一个示例代码: ```java TextView textView = findViewById(R.id.textView); String text = "Hello World!"; // 创建一个 SpannableString 对象 SpannableString spannableString = new SpannableString(text); // 定义渐变起始颜色和结束颜色 int startColor = Color.RED; int endColor = Color.BLUE; // 创建一个 ForegroundColorSpan 对象,并将其应用到 SpannableString ForegroundColorSpan colorSpan = new ForegroundColorSpan(startColor); spannableString.setSpan(colorSpan, 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 创建一个渐变效果的 Span Shader shader = new LinearGradient(0, 0, 0, textView.getPaint().getTextSize(), startColor, endColor, Shader.TileMode.CLAMP); ShaderSpan shaderSpan = new ShaderSpan(shader); // 将渐变效果的 Span 应用到 SpannableString spannableString.setSpan(shaderSpan, 0, text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); // 设置 TextView 的文本为 SpannableString textView.setText(spannableString); ``` 在上面的示例中,我们首先创建了一个 SpannableString 对象,然后定义了起始颜色和结束颜色。接着,我们创建了一个 ForegroundColorSpan 对象,并将其应用到 SpannableString 中,以实现初始字体颜色。然后,我们创建了一个 LinearGradient 对象,并使用 ShaderSpan 来应用渐变效果。最后,我们将 SpannableString 设置为 TextView 的文本,实现字体颜色渐变的效果。 请注意,这种方式只能实现单一的线性渐变效果。如果你想要实现更复杂的渐变效果,可能需要自定义 View 或使用第三方库。 希望这个示例对你有帮助。如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值