Androidレイアウトファイルで下線付きテキストを定義するにはどうすればよいですか?xml
27 に答える
、 、などのHTML タグをサポートする文字列リソースxml ファイルを使用している場合に実現できます。<b></b>
<i></i>
<u></u>
<resources>
<string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
コードから何かに下線を引きたい場合:
TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);
あなたはで試すことができます
textview.setPaintFlags(textview.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
上記の「受け入れられた」回答は機能しません(のような文字列を使用しようとするとtextView.setText(Html.fromHtml(String.format(getString(...), ...)))
.
ドキュメントに記載されているように、内側のタグの開き括弧 (html エンティティ エンコード) を でエスケープする必要があります<
。たとえば、結果は次のようになります。
<resource>
<string name="your_string_here">This is an <u>underline</u>.</string>
</resources>
次に、コードで次のようにテキストを設定できます。
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(String.format(getString(R.string.my_string), ...)));
Strings.xml ファイルの内容:
<resource>
<string name="my_text">This is an <u>underline</u>.</string>
</resources>
レイアウト xml ファイルは、以下に示すように、textview の以下のプロパティで上記の文字列リソースを使用する必要があります。
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/my_text"
android:selectAllOnFocus="false"
android:linksClickable="false"
android:autoLink="all"
/>
Button と TextView の場合、これが最も簡単な方法です。
ボタン:
Button button = (Button) findViewById(R.id.btton1);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
テキストビュー:
TextView textView = (TextView) findViewById(R.id.textview1);
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Kotlinでは拡張機能が使えます。これはコードからのみ使用でき、xml からは使用できません。
fun TextView.underline() {
paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG
}
使用法:
tv_change_number.underline()
tv_resend_otp.underline()
これが遅い答えであることは知っていますが、かなりうまくいく解決策を思いつきました...コード内のテキストに下線を引くためのアンソニー・フォーロニーからの答えを受け取り、それを処理する TextView のサブクラスを作成しました。次に、下線付きの TextView が必要なときはいつでも、XML でサブクラスを使用できます。
作成したクラスは次のとおりです。
import android.content.Context;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextWatcher;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created with IntelliJ IDEA.
* User: Justin
* Date: 9/11/13
* Time: 1:10 AM
*/
public class UnderlineTextView extends TextView
{
private boolean m_modifyingText = false;
public UnderlineTextView(Context context)
{
super(context);
init();
}
public UnderlineTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public UnderlineTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}
private void init()
{
addTextChangedListener(new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
//Do nothing here... we don't care
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
//Do nothing here... we don't care
}
@Override
public void afterTextChanged(Editable s)
{
if (m_modifyingText)
return;
underlineText();
}
});
underlineText();
}
private void underlineText()
{
if (m_modifyingText)
return;
m_modifyingText = true;
SpannableString content = new SpannableString(getText());
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
setText(content);
m_modifyingText = false;
}
}
さて...下線付きのテキストビューをXMLで作成したいときはいつでも、次のようにするだけです:
<com.your.package.name.UnderlineTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="This text is underlined"
android:textColor="@color/blue_light"
android:textSize="12sp"
android:textStyle="italic"/>
この XML スニペットにオプションを追加して、私の例がテキストの色、サイズ、スタイルの変更に対応していることを示しました...
お役に立てれば!
サミュエルの答えを単純化しました:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--https://stackoverflow.com/a/40706098/4726718-->
<item
android:left="-5dp"
android:right="-5dp"
android:top="-5dp">
<shape>
<stroke
android:width="1.5dp"
android:color="@color/colorAccent" />
</shape>
</item>
</layer-list>