Android TextView 텍스트가 래핑되지 않음
누구든지 텍스트에 무슨 문제가 있는지 말해 줄 수 있습니까? 한 줄보다 긴 텍스트는 다음 줄로 줄 바꿈되지 않고 화면을 넘어갑니다.
다음은 코드입니다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dip">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip">
<TextView
android:id="@+id/reviewItemEntityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="event/venue"
android:textColor="@color/maroon"
android:singleLine="true"
android:ellipsize="end"
android:textSize="14sp"
android:textStyle="bold"
android:layout_weight="1" />
<ImageView
android:id="@+id/reviewItemStarRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:src="@drawable/title_1_star" />
</LinearLayout>
<TextView
android:id="@+id/reviewItemDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Description comes here"
android:textSize="12sp"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
내가 직접 고쳤어, 열쇠는 android:width="0dip"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="4dip"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="4dip">
<TextView
android:id="@+id/reviewItemEntityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/maroon"
android:singleLine="true"
android:ellipsize="end"
android:textSize="14sp"
android:textStyle="bold"
android:layout_weight="1" />
<ImageView
android:id="@+id/reviewItemStarRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
</LinearLayout>
<TextView
android:id="@+id/reviewItemDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:width="0dip" />
</LinearLayout>
<ImageView
android:id="@+id/widget01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_nxt"
android:layout_gravity="center_vertical"
android:paddingRight="5dip" />
</LinearLayout>
이 질문에 대한 유일한 정답은 부모를 적절한 너비 (이 경우 FILL_PARENT, WRAP_CONTENT)로 설정하고 android:layout_weight=1래핑해야하는 textview에 사용해야 한다는 것입니다.
SingleLine 기본적으로 켜져 있으므로 변경하지 않습니다.
width0 픽셀로 설정이 작동하지만 좋은 솔루션은 아닙니다.
xml을 사용하는 몇 가지 예 (이번에는 tableview에서) :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="*"
android:id="@+id/tableLayout1">
<TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="test1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="0" />
<TextView android:layout_weight="1"
android:text="test2 very long text that needs to be wrapped properly using layout_weight property and ignoring singleline since that is set by default..."
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</LinearLayout>
코드에서 이것을 설정하려면 1로 설정된이 예제에서와 같이 layout_weight를 세 번째 매개 변수로 찾고 있습니다.
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label = new TextView(getApplicationContext());
label.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1f));
2 개의 매개 변수를 사용해야합니다.
android:ellipsize="none": 텍스트가 textview 너비에서 잘리지 않습니다.android:scrollHorizontally="false"텍스트는 필요한만큼 줄 바꿈합니다.
xml 파일에서 사용하기에 충분합니다.
android:singleLine="false".
그것이 효과가 있기를 바랍니다.
모두 제일 좋다!
.NET Framework를 사용할 때 이러한 솔루션을 사용할 수 없습니다 TableLayout>TableRow>TextView. 그런 다음 TableLayout.shrinkColumns="*". 효과가 있었던 유일한 다른 해결책은 TextView를 강제하는 layout_width:250px것이었지만 너비를 강제하는 것을 좋아하지 않습니다.
테이블로 작업하는 경우 다음과 같이 시도하십시오.
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*">
당신이 필요합니다 shrinkColumns="*"
이것은 분명히 <LinearLayout>. 그래서 뭔가<LinearLayout> <TableLayout> <TableRow> <TextView>
참조 :
http://code.google.com/p/android/issues/detail?id=4000
누군가에게 도움이되기를 바랍니다.
레이아웃 매개 변수 중 하나가 코드에서 잘못되었습니다. 첫 번째 TextView에서
android:layout_width="wrap_content"
로 변경
android:layout_width="fill_parent"
화면 너비 크기를 벗어난 텍스트는 다음 줄로 줄 바꿈되고 설정 android:singleline="false"됩니다.
텍스트보기의 높이 android:minHeight="some pixes"또는 android:width="some pixels". 문제가 해결됩니다.
내 경우에는 입력 유형을 제거하는 것이 트릭을 수행했습니다. 내 textview가 한 줄에 고정되어 있고 래핑되지 않았기 때문에 android : inputType = "textPostalAddress"를 사용하고 있었기 때문에 문제가 해결되었습니다.
저는 Android (및 GUI) 초보자이지만 소프트웨어에 대한 경험이 많습니다. 몇 가지 자습서를 살펴 봤는데 이것이 제 이해입니다.
layout_width 및 layout_height는 TextView의 속성입니다. 이것은 TextView가 어떻게 형성되어야하는지에 대한 지침이며, TextView 내에서 콘텐츠를 처리하는 방법을 언급하지 않습니다.
"fill_parent"를 사용하는 경우 TextView가 부모보기에 상대적인 모양을 지정해야하며 채워야합니다.
"wrap_content"를 사용하는 경우 부모 뷰를 무시하고 TextView의 내용이 모양을 정의하도록해야합니다.
나는 이것이 혼란스러운 점이라고 생각합니다. "wrap_content"는 TextView에 내용을 관리하는 방법 (줄을 감싸는 방법)을 알려주는 것이 아니라 내용에 상대적인 모양을 지정해야한다는 것을 의미합니다. 이 경우 새 줄 문자가 없으면 모든 텍스트가 한 줄에 있도록 자체 모양이 형성됩니다 (불행히도 상위 항목이 넘칩니다).
부모를 가로로 채우고 내용을 세로로 감싸기를 원한다고 생각합니다.
비록 이것은 오래된 실이지만 도움이 된 경험을 공유하고 싶습니다. 내 응용 프로그램은 OS 2.0 및 4.0 이상에서 잘 작동했지만 OS 3.x를 실행하는 HTC 전화의 경우 텍스트가 래핑되지 않았습니다. 나를 위해 일한 것은 이러한 태그를 모두 포함하는 것이 었습니다.
android:maxLines="100"
android:scrollHorizontally="false"
둘 중 하나를 제거하면 OS 3.0 장치에서만 작동하지 않았습니다. "ellipsize"매개 변수에는 중립적 인 효과가 있습니다. 아래는 전체 textview 태그입니다.
<TextView
android:id="@+id/cell_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:maxLines="100"
android:scrollHorizontally="false"
android:textStyle="normal"
android:textSize="11sp"
android:textColor="@color/listcell_detail"/>
이것이 누군가를 도울 수 있기를 바랍니다.
이 속성을 넣습니다.
android:inputType="textMultiLine"
내 TextView에 줄 바꿈이 있고 사용자는 새 줄을 입력 할 수 있습니다.
================================================ ==========
이 게시물에 올 때 여러 줄을 표시 할 수있는 Big TextView를 만들 수 있으므로 이러한 속성도 필요할 수 있습니다.
android:layout_height="Xdp" //where X is a number depends on how big Textview you want
android:gravity="top" //in order to make your text start from the top of your TextView.
즉 높이를 높이십시오. android : height = "Some size", 잘 작동합니다.
두 개의 수평 가중치 TextView가 텍스트를 감싸지 않는 비슷한 문제가 발생했습니다. 나중에 문제가 내 viewparents 부모가 match_parent 대신 wrap_content를 가지고 있기 때문이라는 것을 알게되었습니다.
I think it depends on the particular combination of layouts in your display. Some flags may get overridden or ignored. I have a TabHost with tabs, each tab is a list of tables. So it is a tab of ListView, each row being a TableLayout of TextView. I tried the fixes listed above and none of them worked.
I know, that in question it is correct, but in my case, the problem was in setting textSize property in 'dp' - I've changed it to 'sp' and it works fine.
I used android:ems="23" to solve my problem. Just replace 23 with the best value in your case.
<TextView
android:id="@+id/msg"
android:ems="23"
android:text="ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab "
android:textColor="@color/white"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In my case, with a TableRow > ScrollView > TextView nesting, I solved the problem by setting android:layout_width to fill_parent on TableRow, and to wrap_content on ScrollView and TextView.
you have to use android:singleLine="false" in ur TextView tags.
I finally managed to add some pixels to the height of the TextView to solve this issue.
First you need to actually get the height of the TextView. It's not straightforward because it's 0 before it's already painted.
Add this code to onCreate:
mReceiveInfoTextView = (TextView) findViewById(R.id.receive_info_txt);
if (mReceiveInfoTextView != null) {
final ViewTreeObserver observer = mReceiveInfoTextView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int height = mReceiveInfoTextView.getHeight();
int addHeight = getResources().getDimensionPixelSize(R.dimen.view_add_height);
mReceiveInfoTextView.setHeight(height + addHeight);
// Remove the listener if possible
ViewTreeObserver viewTreeObserver = mReceiveInfoTextView.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.removeOnGlobalLayoutListener(this);
}
}
});
}
You need to add this line to dimens.xml
<dimen name="view_add_height">10dp</dimen>
Hope it helps.
I just removed android:lines="1" and added android:maxLines="2", this got the text to wrap automatically. The problem was the android:lines attribute. That causes the text wrapping to not happen.
I didnt have to use maxEms or singleLine="false" (deprecated API) to fix this.
I've spent hours to figure out that in the text I was trying to display contained a single quote (in string.xml) so I just escaped it with a backslash and it worked nicely => the height of the TextView was correctly wrapping text:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-smallcaps"
android:text="@string/instructions"
android:textAlignment="center"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="@+id/keyword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/defaultKeyword"
android:textSize="22sp"
/>
I'm using constraint layout mostly.
1) android:layout_width="match_parent" = tries to stretch to meet edges
2) android:layout_width="wrap_content" = based solely on the input text without regard for other views nearby. for example adding android:textAlignment="center" will change the shape of the text
3) android:padding="12dp" android:layout_width="0dp" android:layout_weight="1" android:singleLine="false"
= The text will fold to accommodate nearby layouts without regard to the text itself
I added a \n in the middle of my string and it looked okay.
To wrap the text and to put the text in next line we sholud use the "\n" i.e new line character in the layout xml file and check tht change on the emulator not on the layout screen.
참고URL : https://stackoverflow.com/questions/2197744/android-textview-text-not-getting-wrapped
'Programing' 카테고리의 다른 글
| Spark 데이터 프레임이 비어 있는지 확인하는 방법 (0) | 2020.10.21 |
|---|---|
| 힙 메모리 (malloc / new)를 사용하면 비 결정적 프로그램이 생성됩니까? (0) | 2020.10.21 |
| Imagemagick : 고정 너비, 비례 높이로 변환 (0) | 2020.10.21 |
| div의 요소를 서로 균등하게 배포하는 방법은 무엇입니까? (0) | 2020.10.21 |
| iOS 8 iPhone의 UIPopoverPresentationController (0) | 2020.10.21 |