我是 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/