Android TextView에서 줄 바꿈을 추가하는 방법은 무엇입니까?
TextView에 줄 바꿈을 추가하려고합니다.
제안 된 \ n을 시도했지만 아무것도하지 않습니다. 문자를 설정하는 방법은 다음과 같습니다.
TextView txtSubTitle = (TextView)findViewById(r.id.txtSubTitle);
txtSubTitle.setText(Html.fromHtml(getResources().getString(R.string.sample_string)));
이것은 내 문자열입니다. <string name="sample_string">some test line 1 \n some test line 2</string>
다음과 같이 표시되어야합니다.
some test line 1
some test line 2
그러나 다음과 같이 표시 some test line 1 some test line 2
됩니다..
뭔가 빠졌습니까?
\n
다음과 같이 나를 위해 작동합니다.
<TextView android:text="First line\nNext line"
알았어.
<string name="sample_string"><![CDATA[some test line 1 <br />some test line 2]]></string>
따라서 CDATA의 랩이 필요하며 내부에 html 태그로 추가됩니다.
Android 버전 1.6은 \ r \ n을 인식하지 못합니다. 대신 다음을 사용하십시오. System.getProperty ( "line.separator")
String s = "Line 1"
+ System.getProperty ("line.separator")
+ "Line 2"
+ System.getProperty ("line.separator");
줄 바꿈 (\ n) 은 문자열 리소스 값을 다음과 같이 따옴표로 묶은 경우 에만 작동합니다.
<string name="sample_string">"some test line 1 \n some test line 2"</string>
다음과 같이 따옴표없이 넣으면 줄 바꿈을하지 않습니다.
<string name="sample_string">some test line 1 \n some test line 2</string>
예, 그렇게 쉽습니다.
위의 모든 것을 시도했지만 내 자신에 대한 조사를 수행하여 줄 바꿈 이스케이프 문자를 렌더링하는 다음과 같은 솔루션을 만들었습니다.
string = string.replace("\\\n", System.getProperty("line.separator"));
replace 방법을 사용하면 이스케이프 된 줄 바꿈 을 필터링해야합니다 (예 : '\\ n')
그런 다음 줄 바꿈 '\ n'이스케이프 문자의 각 인스턴스는 실제 줄 바꿈으로 렌더링됩니다.
이 예에서는 JSON 형식의 데이터와 함께 Google Apps Scripting noSQL 데이터베이스 (ScriptDb)를 사용했습니다.
건배 : D
이 문제에는 두 가지 방법이 있습니다. 문자열을 원시 문자열로 사용하는 경우 줄 바꿈 문자를 사용해야합니다. 예를 들어로 파싱하여 html로 사용하는 경우 Html.fromString
두 번째 변형이 더 좋습니다.
1) 줄 바꿈 문자 \n
<string name="sample> This\nis a sample</string>
2) HTML 개행 태그 <br>
또는<br />
<string name="sample> This<br>is a sample</string>
이것은 나를 위해 일했다
android:text="First \n Second"
이것은 나를 위해 일했습니다. 아마도 누군가이 도움이 될 것입니다.
TextView textField = (TextView) findViewById(R.id.textview1);
textField.setText("First line of text" + System.getProperty("line.separator") + "Linija 2");
XML을 사용하여 TextView 사용 android:singleLine = "false"
또는 Java 를 선언 하는 경우txtSubTitle.setSingleLine(false);
Android Studio 0.8.9를 사용했습니다. 나를 위해 일한 유일한 방법은을 사용하는 것 \n
입니다. CDATA로 포장 <br>
하거나 <br />
작동 하지 않았습니다 .
나는 다음을 사용한다 :
YOUR_TEXTVIEW.setText("Got some text \n another line");
매우 쉬운 : "\ n"사용
String aString1 = "abcd";
String aString2 = "1234";
mSomeTextView.setText(aString1 + "\n" + aString2);
\ n은 'LF'또는 줄 바꿈 인 ASCII 문자 0xA에 해당합니다.
\ r은 ASCII 문자 0xD에 해당하며 'CR'또는 캐리지 리턴입니다.
이것은 첫 번째 타자기에서 거슬러 올라갑니다. 여기서 줄 바꿈 만하고 줄 바꿈 만 수행하거나 줄 바꿈 + 캐리지 리턴 (줄의 시작 부분으로 이동)을 선택할 수 있습니다
안드로이드 / 자바에서 \ n은 캐리지 리턴 + 줄 바꿈에 해당합니다. 그렇지 않으면 같은 줄을 '덮어 씁니다'
Try to double-check your localizations. Possible, you trying to edit one file (localization), but actually program using another, just like in my case. The default system language is russian, while I trying to edit english localization.
In my case, working solution is to use "\n" as line separator:
<string name="string_one">line one.
\nline two;
\nline three.</string>
Also you can add "<br/>"
instead of \n.
It's HTML escaped code for <br/>
And then you can add text to TexView:
articleTextView.setText(Html.fromHtml(textForTextView));
You could also use the String-Editor of Android Studio, it automatically generates line brakes and stuff like that...
As Html.fromHtml
deprecated I simply I used this code to get String2 in next line.
textView.setText(fromHtml("String1 <br/> String2"));
.
@SuppressWarnings("deprecation")
public static Spanned fromHtml(String html){
Spanned result;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
result = Html.fromHtml(html,Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(html);
}
return result;
}
The most easy way to do it is to go to values/strings (in your resource folder)
Declare a string there:
<string name="example_string">Line 1\Line2\Line n</string>
And in your specific xml file just call the string like
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example_string" />
I found another method: Is necessary to add the "android:maxWidth="40dp"" attribute. Of course, it may not work perfectly, but it gives a line break.
\n was not working for me. I was able to fix the issue by changing the xml to text and building the textview text property like below.
android:text="Line 1
Line 2
Line 3
DoubleSpace"
Hopefully This helps those who have said that \n did not work for them.
I'm reading my text from a file, so I took a slightly different approach, since adding \n to the file resulted in \n appearing in the text.
final TextView textView = (TextView) findViewById(R.id.warm_up_view);
StringBuilder sb = new StringBuilder();
Scanner scanner = new Scanner(getResources().openRawResource(R.raw.warm_up_file));
while (scanner.hasNextLine()) {
sb.append(scanner.nextLine());
sb.append("\n");
}
textView.setText(sb.toString());
In my case, I solved this problem by adding the following:
android:inputType="textMultiLine"
Maybe you are able to put the lf into the text, but it doesn't display? Make sure you have enough height for the control. For example:
Correct:
android:layout_height="wrap_content"
May be wrong:
android:layout_height="10dp"
참고URL : https://stackoverflow.com/questions/5382490/how-to-add-a-line-break-in-an-android-textview
'IT story' 카테고리의 다른 글
iOS 용 이벤트 처리-hitTest : withEvent : 및 pointInside : withEvent :는 어떤 관련이 있습니까? (0) | 2020.06.17 |
---|---|
SQL Server 복원 오류-액세스가 거부되었습니다 (0) | 2020.06.17 |
Swift에서 Array의 처음 5 객체를 반환하는 방법은 무엇입니까? (0) | 2020.06.17 |
리눅스에서 디렉토리를 반복하는 방법? (0) | 2020.06.17 |
date_modify를 DateTime 객체로 사용하여 PHP에서 현재 달의 첫날 (0) | 2020.06.17 |