Android 레이아웃 xml
파일에서 밑줄이 그어진 텍스트를 정의하려면 어떻게 해야 하나요?
문자열 리소스**](http://developer.android.com/guide/topics/resources/available-resources.html#stringresources) xml 파일을 사용하는 경우 <b></b>
, <i></i>
및 <u></u>
같은 HTML 태그를 지원하므로 이 작업을 수행할 수 있습니다.
<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.setText(Html.fromHtml(String.format(getString(...), ...)))
와 같은 문자열을 사용하려고 할 때).
문서]1에 명시된 대로 내부 태그의 이스케이프(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
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 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);
가장 최근의 외곽진입 드로잉 밑줄 텍스트 설명자 로맹 담당이잖아요 [중간] [1] 에서 사용할 수 있는 소스 코드 진단트리로 깃허브. 이 예제 응용 프로그램 두 가지 가능한 구현을 제공합니다.
[1]: # .d76mvs6i3 https://medium.com/google-developers/a-better-underline-for-android-90ba3a2e4fb
하지만 난 말 그러니까말이야 이는 오토메이티드 꽤 잘 사용할 수 있는 해결책을 마련했습니다. 내가 만든 데 대한 답을 앤서니 포로니 밑줄 텍스트는 코드 및 하위 테스트비에프 있는 처리하는 당신꺼에요. 그런 다음 그 하위 클래스를 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 텍스트 색상, 크기, uxfs 수행됨 스니핏 내 스타일.
이를 통해 희망을!
체크아웃합니다 클릭가능 (b) 버튼를 강조한 스타일.
<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" />
<string name="btn_add_contact"><u>Add new contact</u></string>
결과:.
내가 만드는 데 사용하는 이 xml 드로어 블 드로어 블 배경으로 렌더링합니다 내 테스트비에프 아래쪽 경계 및 적용
<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>
TextView tv = findViewById(R.id.tv);
tv.setText("some text");
setUnderLineText(tv, tv.getText().toString());
setUnderLineText(tv, "some");
>. 또한, 단추, 확인란 에디트스트 같은 지원부에서는 테스트비에프 차일즈
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);
}
}
}
확인 할 수 있도록 텍스트 또는 밑줄입니다 여러 부분을 밑줄입니다 클릭가능 (b) 이 제 대답은 테스트비에프
단순하고 유연한 솔루션에 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" />
또 다른 방법은 아래와 같이 사용자 정의 보기를 해 주는 테스트비에프 만들기
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"
/>
[축구] 내가 간편화된 (https://stackoverflow.com/a/40706098/4726718) & # 39 의 대답:
<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>
이 코드를 시도하시겠습니까
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)));
굿 럭!
>. 스테링스.1믈 HTML 밑줄입니다 샘플링합니다
>. 샘프레티스트비에웨인스테스트 (R. string.sample_string);
또한 다음과 같은 코드를 밑줄입니다 인쇄인쇄 않습니다.
String sampleString = getString(R.string.sample_string);
sampleTextView.setText(sampleString);
서식 있는 텍스트 포맷 대신 다음과 같은 코드를 사용하여 유지할 수 있습니다.
CharSequence sampleString = getText(R.string.sample_string);
sampleTextView.setText(sampleString);
, (int) 또는 getText 제스트링 < b> " 사용할 수 있습니다 (int) 읽어들이려면 구체화하십시오. getText (int) 를 적용, 스타일, 다른 서식 있는 텍스트 내보내집니다 string." < /b>. 안드로이드 문서용으로.
문서를 참조하십시오. https://developer.android.com/guide/topics/resources/string-resource.html
이렇게 하면 좋겠다.
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>
참고합니다 이스케이프입니다 심볼일 xml 파일