Mic*_*ner 100 android underline textview android-styles
如何在XML文件中为文本加下划线?我找不到一个选项textStyle
.
Geo*_*iou 163
如果您使用的是字符串资源的XML文件(支持HTML标签),这是可以做到用<b> </b>
,<i> </i>
和<u> </u>
.
<resources>
<string name="your_string_here">
This is an <u>underline</u>.
</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
如果你想用代码使用下划线:
TextView tv = (TextView) view.findViewById(R.id.tv);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tv.setText(content);
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
abh*_*hek 55
用这个:
TextView txt = (TextView) findViewById(R.id.Textview1);
txt.setPaintFlags(txt.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Run Code Online (Sandbox Code Playgroud)
Bha*_*vin 21
<resource>
<string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
如果它不起作用那么
<resource>
<string name="your_string_here">This is an <u>underline</u>.</string>
Run Code Online (Sandbox Code Playgroud)
因为"<"可能在某个时候成为关键字.
并用于显示
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.your_string_here)));
Run Code Online (Sandbox Code Playgroud)
首先,转到String.xml文件
您可以在此处添加任何 HTML 属性,例如 、斜体、粗体或下划线。
<resources>
<string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
我使用了下面的方法,它对我有用。下面是 Button 的示例,但我们也可以在 TextView 中使用。
Button btnClickMe = (Button) findViewById(R.id.btn_click_me);
btnClickMe.setPaintFlags(btnClickMe.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Run Code Online (Sandbox Code Playgroud)
另一种方法是创建一个扩展 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);
}
}
Run Code Online (Sandbox Code Playgroud)
以及如何在 xml 中使用它:
<com.myapp.components.LinkTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
130282 次 |
最近记录: |