programing

안드로이드에서 텍스트 보기에 새 줄을 추가하려면 어떻게 해야 합니까?

itsource 2023. 8. 15. 20:17
반응형

안드로이드에서 텍스트 보기에 새 줄을 추가하려면 어떻게 해야 합니까?

다음을 정의하는 경우TextViewxml새 줄을 추가하려면 어떻게 해야 합니까?\n작동하지 않는 것 같습니다.

<TextView
   android:id="@+id/txtTitlevalue"
   android:text="Line1 -\nLine2"
   android:layout_width="54dip"
   android:layout_height="fill_parent"
   android:textSize="11px" />

Visual 편집기를 신뢰하지 않습니다.당신의 코드는 에뮤에서 작동합니다.

시도:

android:lines="2"

\n작동해야 합니다.

해라System.getProperty("line.separator");

내 생각에 이것은 당신의 것과 관련이 있습니다.HTM.fromHtml(subTitle)호출: "\n"은 HTML에 대한 부프키스를 의미하지 않습니다. 시도해 보십시오.<br/>"\n" 대신.

먼저 텍스트 보기에 다음을 입력합니다.

android:maxLines="10"

사용할 경우\n텍스트 보기의 텍스트에 표시됩니다.

maxLines를 사용하면 TextView의 높이가 최대 이 정도가 됩니다.다른 번호를 선택하셔도 됩니다 :)

위의 모든 것을 시도하고, 나 자신의 연구를 통해 라인 피드 이스케이프 문자를 렌더링하기 위한 다음 솔루션을 만들었습니다.

string = string.replace("\\\n", System.getProperty("line.separator"));
  1. 교체 방법을 사용하여 이스케이프 라인 피드를 필터링해야 합니다(예:'\\\n')

  2. 라인 피드의 각 인스턴스만'\n'이스케이프 문자가 실제 라인피드로 렌더링됩니다.

이 예에서는 JSON 형식의 데이터가 포함된 Google Apps Scripting noSQL 데이터베이스(ScriptDb)를 사용했습니다.

건배사 : D

다음 항목을 확인합니다.\n안에 있습니다."\n"그것이 작동하기 위해.

<TextView
   android:id="@+id/txtTitlevalue"
   android:text="Line1: \r\n-Line2\r\n-Line3"
   android:layout_width="54dip"
   android:layout_height="fill_parent"
   android:textSize="11px" />

이게 될 것 같아요.

안드로이드 TV를 사용하는 나의 2센트.

더하다\nXML 문자열로, 읽는 동안:

public void setSubtitle(String subtitle) {
    this.subtitle = subtitle.replace("\\n", System.getProperty("line.separator"));
}

저는 같은 문제를 해결할 뿐입니다. 아래 속성을 xml로 입력합니다.

android:lines="2" android:maxLines="4" android:singleLine="false"

일하다.Html.fromHtml("text1 <br> text2").toString()효과도 있습니다.

이것으로 저의 문제가 해결되었습니다.

stringVar.replaceAll("\\\\n", "\\\n");

이를 위한 한 가지 방법은 다음을 사용하는 것입니다.Html태그:

txtTitlevalue.setText(Html.fromHtml("Line1"+"<br>"+"Line2" + " <br>"+"Line3"));

또한 추가할 수 있습니다.&lt;br&gt;대신에\n.

그런 다음 텍스트를 TexView에 추가할 수 있습니다.

articleTextView.setText(Html.fromHtml(textForTextView));

System.getProperty("line.separator");나를 위한 이 일.

디버깅하면 문자열이 실제로"\ \r\ \n"또는"\ \n"즉, 탈출했습니다.그래서 만약 당신이 그 끈을 마사지한다면, 여분의 것을 없애기 위해.\당신은 당신의 해결책을 갖게 될 것입니다.특히 데이터베이스에서 읽는 경우에는 더욱 그렇습니다.

당신은 그것을 넣을 필요가 있습니다."\n"에서strings.xml페이지 레이아웃에 없는 파일입니다.

보관 필요

1.android:maxLines="no of lines"

2. 및 사용\n다음 줄을 타기 위해.

저에게 해결책은 레이아웃에 다음 행을 추가하는 것이었습니다.

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    ...
    >

그리고 \n은 시각적 편집기에 새 줄로 표시됩니다.도움이 되길 바랍니다!

 android:text="Previous Line &#10; Next Line" 

이것은 효과가 있을 거예요.

이거는.\n서류철에string.xml

<string name="strtextparkcar">press Park my Car to store location \n</string>

사용해 보십시오.

android:text="Lorem Ipsum \nDolor Ait Amet \nLorem Ipsum"

이것은 잘 작동합니다.당신의 앱을 확인하세요.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 1\nText 2\nText 3"/>

시도해 보십시오.

Textview_name.settext("something \n  new line "):

Java 파일에 있습니다.

RuDrA05의 대답은 좋습니다. Eclipse에서 XML을 편집하면 작동하지 않지만 메모장++로 XML을 편집하면 작동합니다.

이클립스 또는 메모장으로 저장된 텍스트 파일을 읽어도 동일한 현상이 발생합니다.

인코딩과 관련이 있을 수 있습니다.

참고: 다음을 사용하여 텍스트 대문자화

android:inputType="textCapCharacters"

또는 비슷한 것이 그것을 멈추는 것처럼 보입니다.\n일 때문에.

▁you▁make▁sure를 사용하고 있는지 확인하세요.your_package.R그리고 아닌android.R

텍스트 보기에서 새 줄의 경우 텍스트 중간에 \n을 추가하면 됩니다.

Layout(을 수직으로 합니다.android:orientation="vertical"하거나 변경할 수 있습니다.TextView 기다하android:singleLine="true"다음 줄을 새로 만듭니다.

프로그래밍 방식: myTV.setText("MyText" + "\n" + myString);

stings.xml에 문자열을 만듭니다.

<resources>
    ...
    <string name="new_line">\u000A</string>
</resources>

그러면 당신은 코드에서 문자열을 참조합니다.

val linkString = "This is in the first line.${getString(R.string.new_line)}This is on the second line."

언급URL : https://stackoverflow.com/questions/2840608/how-do-i-add-a-newline-to-a-textview-in-android

반응형