android - 我们如何以编程方式将颜色设置为 textView 中文本的下划线

标签 android android-studio

我是 Android 新手。我创建了一个表并在其中设置了 textView。我想为我在 textView 中输入的文本添加下划线。我通过以下代码创建了一个下划线:

SpannableString content=new SpannableString(区域) content.setSpan(new UnderlineSpan(),0,Regions.length(),0);

其中 region 是包含文本的字符串。

谁能告诉我如何根据我们对下划线的选择设置颜色?

最佳答案

我遇到了同样的问题,没有直接的方法来做到这一点。您可以在下面查看我的解决方案:

修改布局中的 TextView 如下:

<TextView
            android:id="@+id/residential_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:background="@drawable/only_one_bottom_line"
            android:layout_margin="12dp"
            android:paddingBottom="4dp"
            android:text="test string"
            android:textColor="@drawable/re_option_highlight"
            android:textSize="11sp" />

only_one_bottom_line.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:top="-4dp" android:right="-4dp" android:left="-4dp">
        <shape>
            <solid android:color="@android:color/transparent"/>
            <stroke
                android:width="3dp"
                android:color="#0093dd" >
            </stroke>
        </shape>
    </item>
</layer-list>

This background have only one bottom line, and you can change the color from #00 93dd to any color you want. This solution is more easier to implement than creating a custom text view and making hell lot of changes there.

关于android - 我们如何以编程方式将颜色设置为 textView 中文本的下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37783364/

相关文章:

android - 内存不足加载 imageViews

安卓 : RecyclerView row Visibility not working properly

android - AVD 设置中缺少“快照”和 'Use Host GPU' 选项

android - 无法在 21 以下的 Api 中获取提供商 com.google.firebase.provider.FirebaseInitProvider

java - 如何在 Android Studio 中导入 android.support.v7.app.NotificationCompat.Builder 类

java - 为什么我的扩展 android.app.application 的应用程序类不起作用?

android - 具有 ASTC 8x8 纹理的 glCompressedTexSubImage2D 上的 GL_INVALID_VALUE

android - XAMARIN - 是否可以在我的应用程序中使用默认/系统字体?

android - 在 sqlite 中搜索,基于 String 获取描述

java - 想在android项目中使用com.mcxiaoke.volley