768

Androidレイアウトファイルで下線付きテキストを定義するにはどうすればよいですか?xml

4

27 に答える 27

1267

、 、などの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);
于 2010-03-07T02:29:57.760 に答える
586

あなたはで試すことができます

textview.setPaintFlags(textview.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);
于 2012-06-08T10:44:58.923 に答える
74

上記の「受け入れられた」回答は機能しません(のような文字列を使用しようとするとtextView.setText(Html.fromHtml(String.format(getString(...), ...))).

ドキュメントに記載されているように、内側のタグの開き括弧 (html エンティティ エンコード) を でエスケープする必要があります&lt;。たとえば、結果は次のようになります。

<resource>
    <string name="your_string_here">This is an &lt;u&gt;underline&lt;/u&gt;.</string>
</resources>

次に、コードで次のようにテキストを設定できます。

TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(String.format(getString(R.string.my_string), ...)));
于 2012-03-31T10:22:00.920 に答える
68

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"
    />
于 2012-04-11T06:04:25.933 に答える
67

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);
于 2015-09-04T08:41:53.940 に答える
35

Kotlinでは拡張機能が使えます。これはコードからのみ使用でき、xml からは使用できません。

fun TextView.underline() {
    paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG
}

使用法:

 tv_change_number.underline()
 tv_resend_otp.underline()
于 2018-08-16T11:36:34.923 に答える
11

これが遅い答えであることは知っていますが、かなりうまくいく解決策を思いつきました...コード内のテキストに下線を引くためのアンソニー・フォーロニーからの答えを受け取り、それを処理する 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 スニペットにオプションを追加して、私の例がテキストの色、サイズ、スタイルの変更に対応していることを示しました...

お役に立てれば!

于 2013-09-11T07:41:24.377 に答える
11

下線付きのテキストを描画する最新のアプローチは、Romain Guy によってメディアで説明されており、ソース コードはGitHubで入手できます。このサンプル アプリケーションは、2 つの可能な実装を公開します。

  • API レベル 19 を必要とするパスベースの実装
  • API レベル 1 を必要とする地域ベースの実装

ここに画像の説明を入力

于 2016-06-30T08:59:44.257 に答える
5

サミュエルの答えを単純化しました:

<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>
于 2018-04-16T22:31:01.197 に答える