你如何在Android XML中强调文本?
如何在XML文件中为文本加下划线? 我无法在textStyle找到一个选项。
9个解决方案
149 votes
如果您使用的是字符串资源xml文件(支持HTML标记),则可以使用 , 和 完成。
This is an underline.
如果你想用代码使用下划线:
TextView tv = (TextView) view.findViewById(R.id.tv);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tv.setText(content);
希望这可以帮助
George Artemiou answered 2019-08-19T14:28:22Z
53 votes
用这个:
TextView txt = (TextView) findViewById(R.id.Textview1);
txt.setPaintFlags(txt.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
abh22ishek answered 2019-08-19T14:28:46Z
19 votes
This is an underline.
如果它不起作用那么
This is an <u>underline</u>.
因为&#34;&lt;&#34; 可能在某个时候是关键字。
并用于显示
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.your_string_here)));
Bhavin answered 2019-08-19T14:29:29Z
3 votes
首先转到String.xml文件
你可以在这里添加任何html属性,如斜体或粗体或下划线。
This is an underline.
Xar E Ahmer answered 2019-08-19T14:30:05Z
2 votes
另一种方法是创建一个扩展TextView的自定义组件。 它适用于需要多个带下划线的TextView的情况。
这是组件的代码:
package com.myapp.components;
import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
public class LinkTextView extends AppCompatTextView {
public LinkTextView(Context context) {
this(context, null);
}
public LinkTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setText(CharSequence text, BufferType type) {
SpannableString content = new SpannableString(text);
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
super.setText(content, type);
}
}
以及如何在xml中使用它:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
Tharkius answered 2019-08-19T14:30:48Z
1 votes
我使用下面的方法,它为我工作。 下面是Button的示例,但我们也可以在TextView中使用。
Button btnClickMe= (Button) findViewById(R.id.btn_click_me);
btnClickMe.setPaintFlags(btnClickMe.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Hitesh Dhamshaniya answered 2019-08-19T14:31:15Z
1 votes
您可以使用下面的标记,但请注意,如果将textAllCaps设置为true,则将删除下划线效果。
I am underlined.
注意
使用带有标记的字符串(login_change_settings)的textAllCaps; 标记将通过大写转换来删除
textAllCaps文本转换将最终在CharSequence上调用toString,这具有删除任何标记的净效果,例如。 此检查查找包含标记的字符串的用法,该标记也指定textAllCaps = true。
Tebo answered 2019-08-19T14:31:59Z
0 votes
完成Bhavin的回答。例如,添加下划线或重定向。
((TextView) findViewById(R.id.tv_cgu)).setText(Html.fromHtml(getString(R.string.upload_poi_CGU)));
conditions générales]]>
你可以在这里知道兼容的标签:[http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html]
Anthone answered 2019-08-19T14:32:35Z
0 votes
有多种方法可以在Android TextView中实现带下划线的文本。
1.This is my underlined text 要么
I just want to underline this word
你可以通过编程方式做同样的事情。
`textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);`
3.可以通过创建SpannableString然后将其设置为TextView文本属性来完成
SpannableString text = new SpannableString("Voglio sottolineare solo questa parola");
text.setSpan(new UnderlineSpan(), 25, 6, 0);
textView.setText(text);
Anandharaj R answered 2019-08-19T14:33:28Z