textview下划线未在真实设备上显示

时间:2018-01-29 11:36:08

标签: android textview underline

我使用的TextView使用以下字符串: android:text="@string/menu"

该字符串在<string name="menu"><u>MENU</u></string>文件中定义为strings.xml

但是,下划线仅显示在虚拟设备中,但不显示在预览中,更重要的是,不显示在真实设备中。 有什么问题?

3 个答案:

答案 0 :(得分:0)

您可以使用SpannableString类中的UnderlineSpan为文本加下划线:

SpannableString content = new SpannableString(<your text>);
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);

然后使用textView.setText(content);

或者

您需要使用Html.fromHtml()在XML字符串中使用HTML。只需在布局XML中引用带有HTML的String,就无法在JVM中使用。

SDK&lt; Android Nougat使用:

myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));

SDK&gt; = Android Nougat:

 myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));

要区分Android版本,请使用Build.VERSION.SDK_INT >= Build.VERSION_CODES.N

答案 1 :(得分:0)

或只是使用此

textview.setPaintFlags(textview.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);

答案 2 :(得分:0)

我遇到了类似的问题:在某些设备上,下划线效果很好,但在其他设备上(诺基亚 10、阿尔卡特),根本看不到下划线。无论是使用 Spannables、Paintflags、Html。我想这与提供程序的硬件或 Android 版本有关 - 不管怎样,我需要一个解决方案,我想出了以下内容:

我在实际文本下使用了一个非常细的视图,它只是渲染了一条白线。我将其可见性设置为 VIEW.Visible 或 VIEW.Gone 以添加下划线或删除下划线。为了让这个视图靠近文本,我使用了约束布局并将其约束在视图的底部、左侧和右侧。 (但也许线性布局也适用,对我来说,Contraint 布局很灵活,我喜欢这样。)

 <androidx.constraintlayout.widget.ConstraintLayout          
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/actual_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <View
                android:id="@+id/underline_hack"
                android:background="#ffffff"
                app:layout_constraintTop_toBottomOf="@id/actual_text"
                app:layout_constraintStart_toStartOf="@id/actual_text"
                app:layout_constraintEnd_toEndOf="@id/actual_text"
                android:layout_width = "0dp"
                android:layout_height="1.5dp"
                android:layout_marginTop="2.5dp"
                android:visibility="gone"/>
   </androidx.constraintlayout.widget.ConstraintLayout>
相关问题