ฉันจะกำหนดข้อความที่ขีดเส้นใต้ในxmlไฟล์เลย์เอาต์ Android ได้อย่างไร
ฉันจะกำหนดข้อความที่ขีดเส้นใต้ในxmlไฟล์เลย์เอาต์ Android ได้อย่างไร
คำตอบ:
มันสามารถเกิดขึ้นได้ถ้าคุณกำลังใช้ทรัพยากรสตริงไฟล์ xml ซึ่งรองรับ HTML แท็กเช่น<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);<u>แท็กที่อ้างอิงผ่านไม่ทำงานเช่นบางครั้งหากคุณใช้แบบอักษรที่กำหนดเอง อย่างไรก็ตามการเขียนโปรแกรมพื้นฐานโดยUnderlineSpanยังไม่ได้ล้มเหลวกับฉันดังนั้นฉันจะแนะนำให้มันเป็นทางออกที่น่าเชื่อถือที่สุด
                    คุณสามารถลองด้วย
textview.setPaintFlags(textview.getPaintFlags() |   Paint.UNDERLINE_TEXT_FLAG);| Paint.ANTI_ALIAS_FLAGเช่นกันหรือข้อความของคุณจะดูคม สิ่งนี้ปรากฏใน API ที่ต่ำกว่าบ่อยกว่าไม่
                    textView.paintFlags = textView.paintFlags or 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"
    />สำหรับปุ่มและ TextView นี่เป็นวิธีที่ง่ายที่สุด:
ปุ่ม:
Button button = (Button) findViewById(R.id.btton1);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);TextView:
TextView textView = (TextView) findViewById(R.id.textview1);
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);myTextView.setText(Html.fromHtml("<p><u>I am Underlined text</u></p>"));
มันช้าไปหน่อย แต่อาจมีประโยชน์สำหรับใครบางคน
ในฟังก์ชั่นการขยาย Kotlin สามารถใช้ได้ สามารถใช้จากรหัสไม่ใช่ xml
fun TextView.underline() {
    paintFlags = paintFlags or Paint.UNDERLINE_TEXT_FLAG
}การใช้งาน:
 tv_change_number.underline()
 tv_resend_otp.underline()ตรวจสอบรูปแบบปุ่มแบบคลิกได้ที่ขีดเส้นใต้:
<TextView
    android:id="@+id/btn_some_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_add_contact"
    android:textAllCaps="false"
    android:textColor="#57a0d4"
    style="@style/Widget.AppCompat.Button.Borderless.Colored" />strings.xml:
<string name="btn_add_contact"><u>Add new contact</u></string>ผลลัพธ์:
วิธีการล่าสุดของการวาดภาพขีดเส้นใต้ข้อความอธิบายโดย Romain ผู้ชายในขนาดกลางที่มีรหัสแหล่งที่มาที่มีอยู่บนGitHub แอปพลิเคชันตัวอย่างนี้แสดงการใช้งานที่เป็นไปได้สองแบบ:
ฉันรู้ว่านี่เป็นคำตอบที่ช้า แต่ฉันคิดวิธีแก้ปัญหาที่ใช้งานได้ดี ... ฉันได้รับคำตอบจาก Anthony Forloney สำหรับการขีดเส้นใต้ข้อความในรหัสและสร้าง subclass ของ TextView ที่จัดการสิ่งนั้นให้คุณ จากนั้นคุณสามารถใช้คลาสย่อยใน XML เมื่อใดก็ตามที่คุณต้องการให้มี TextView ที่ขีดเส้นใต้
นี่คือคลาสที่ฉันสร้าง:
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 นี้เพื่อแสดงว่าตัวอย่างของฉันทำงานกับการเปลี่ยนสีของตัวอักษรขนาดและสไตล์ ...
หวังว่านี่จะช่วยได้!
วิธีที่สะอาดกว่า
textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
วิธีนี้คือการใช้ 
textView.getPaint().setUnderlineText(true);
และหากคุณต้องการปิดการขีดเส้นใต้สำหรับมุมมองนั้นในภายหลังเช่นในมุมมองที่นำกลับมาใช้ใหม่ใน RecyclerView textView.getPaint().setUnderlineText(false);
เพียงใช้แอตทริบิวต์ในไฟล์ทรัพยากรสตริงเช่น
<string name="example"><u>Example</u></string>ฉันใช้ drawable xml นี้เพื่อสร้างเส้นขอบด้านล่างและใช้ drawable เป็นพื้นหลังกับมุมมองข้อความของฉัน
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
    <item android:top="-5dp" android:right="-5dp" android:left="-5dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                    android:width="1.5dp"
                    android:color="@color/pure_white" />
        </shape>
    </item>
</layer-list>โซลูชันที่ง่ายและยืดหยุ่นใน xml:
<View
  android:layout_width="match_parent"
  android:layout_height="3sp"
  android:layout_alignLeft="@+id/your_text_view_need_underline"
  android:layout_alignRight="@+id/your_text_view_need_underline"
  android:layout_below="@+id/your_text_view_need_underline"
  android:background="@color/your_color" />อีกวิธีคือสร้างมุมมองที่กำหนดเองที่ขยาย TextView ดังที่แสดงด้านล่าง
public class UnderLineTextView extends TextView {
    public UnderLineTextView(Context context) {
        super(context);
        this.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    }
    public UnderLineTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
    }
}และเพิ่มไปยัง xml ดังที่แสดงด้านล่าง
<yourpackage.UnderLineTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="underline text"
 />ฉันทำให้คำตอบของซามูเอลง่ายขึ้น:
<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>TextView tv = findViewById(R.id.tv);
tv.setText("some text");ขีดเส้นใต้ทั้ง TextView
setUnderLineText(tv, tv.getText().toString());ขีดเส้นใต้บางส่วนของ TextView
setUnderLineText(tv, "some");รองรับ TextView childs เช่น EditText, Button, Checkbox
public void setUnderLineText(TextView tv, String textToUnderLine) {
        String tvt = tv.getText().toString();
        int ofe = tvt.indexOf(textToUnderLine, 0);
        UnderlineSpan underlineSpan = new UnderlineSpan();
        SpannableString wordToSpan = new SpannableString(tv.getText());
        for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
            ofe = tvt.indexOf(textToUnderLine, ofs);
            if (ofe == -1)
                break;
            else {
                wordToSpan.setSpan(underlineSpan, ofe, ofe + textToUnderLine.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
            }
        }
    }ตรวจสอบคำตอบของฉันที่นี่เพื่อให้สามารถขีดเส้นใต้ข้อความหรือขีดเส้นใต้หลายส่วนของ TextView
ลองรหัสนี้
ใน XML
<resource>
 <string name="my_text"><![CDATA[This is an <u>underline</u>]]></string> 
</resources> ในรหัส
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(Html.fromHtml(getString(R.string.my_text)));โชคดี!
sampleTextView.setText(R.string.sample_string);
นอกจากนี้รหัสต่อไปนี้จะไม่พิมพ์ขีดเส้นใต้:
String sampleString = getString(R.string.sample_string);
sampleTextView.setText(sampleString);ให้ใช้รหัสต่อไปนี้แทนเพื่อรักษารูปแบบ Rich Text:
CharSequence sampleString = getText(R.string.sample_string);
sampleTextView.setText(sampleString);"คุณสามารถใช้ getString (int) หรือ getText (int) เพื่อดึงสตริงได้ getText (int) ยังคงใส่สไตล์ข้อความที่หลากหลายที่ใช้กับสตริง" เอกสารประกอบ Android
อ้างถึงเอกสารประกอบ: https://developer.android.com/guide/topics/resources/string-resource.html
ฉันหวังว่านี่จะช่วยได้.
คำตอบที่ได้รับการโหวตอย่างถูกต้องและง่ายที่สุด อย่างไรก็ตามบางครั้งคุณอาจพบว่าไม่ได้ทำงานกับตัวอักษรบางตัว แต่ทำงานกับคนอื่น ๆ (ปัญหาที่ฉันเพิ่งเจอเมื่อพูดภาษาจีน)
โซลูชันไม่ใช้ "WRAP_CONTENT" สำหรับ TextView ของคุณเท่านั้นทำให้ไม่มีพื้นที่เพิ่มเติมสำหรับการวาดเส้น คุณสามารถตั้งค่าความสูงคงที่เป็น TextView ของคุณหรือใช้ android: paddingVertical ด้วย WRAP_CONTENT
HtmlCompat.fromHtml(
                    String.format(context.getString(R.string.set_target_with_underline)),
                    HtmlCompat.FROM_HTML_MODE_LEGACY)
<string name="set_target_with_underline"><u>Set Target<u> </string>หมายเหตุสัญลักษณ์ Escape ในไฟล์ xml
ในการทำเช่นนั้นใน Kotlin:
yourTextView.paint?.isUnderlineText = true
มันค่อนข้างช้าที่จะตอบคำถามนี้ แต่สมมติว่าถ้าใครต้องการได้รับข้อความแบบไดนามิกจากนั้นพวกเขาสามารถใช้หนึ่งบรรทัดที่เรียบง่ายนี้ในรหัส java ของพวกเขาที่ทำงาน:
 textView.setText(Html.fromHtml("<p><u>" + get_name + "</u></p>"));ฉันมีปัญหาที่ฉันใช้แบบอักษรที่กำหนดเองและขีดเส้นใต้ที่สร้างขึ้นพร้อมกับเคล็ดลับไฟล์ทรัพยากร ( <u>Underlined text</u>) ทำงานได้ แต่ Android สามารถเปลี่ยนขีดเส้นใต้เป็นรางตีได้
ผมใช้คำตอบนี้ไปวาดเส้นขอบด้านล่าง TextView ตัวเอง: https://stackoverflow.com/a/10732993/664449 เห็นได้ชัดว่าวิธีนี้ใช้ไม่ได้กับข้อความที่ขีดเส้นใต้หรือข้อความหลายบรรทัด