问题
I have been trying to get a TextView to wrap the text to multiple lines on Android 2.3.3 for a while not but I can't seam to get it working, even on the most basic of levels. No matter what I do the text always just cuts off at the edge of the screen. It works just fine on Android 4 but I would like to target 2.3.3 as well.
I have tried just the basic part here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/mainDescText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/A_MORE_DESCRIPTION_TEXT" />
</LinearLayout>
But that still doesn't wrap the text. My real layout that I am trying to get this to work on is here:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="55dp" >
<TextView
android:id="@+id/mainDescText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:text="@string/A_MORE_DESCRIPTION_TEXT" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp" >
<TextView
android:id="@+id/ageCurrentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ageButton"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/A_MORE_CURRENT_SIGHTINGS_AGE" />
<Button
android:id="@+id/ageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:onClick="loadSightingsAge"
android:text="@string/A_MORE_SIGHTINGS_AGE_BUTTON" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/languageCurrentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/languageButton"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/A_MORE_CURRENT_LANGUAGE" />
<Button
android:id="@+id/languageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:onClick="loadLanguage"
android:text="@string/A_MORE_LANGUAGE_BUTTON" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp" >
<ImageView
android:id="@+id/redPinImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/red_pin"
android:contentDescription="@string/A_MORE_RED_PIN_IMAGE" />
<TextView
android:id="@+id/redPinText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/redPinImage"
android:text="@string/A_MORE_RED_PIN_DESCRIPTION" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp" >
<ImageView
android:id="@+id/yellowPinImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/yellow_pin"
android:contentDescription="@string/A_MORE_YELLOW_PIN_IMAGE" />
<TextView
android:id="@+id/yellowPinText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/yellowPinImage"
android:text="@string/A_MORE_YELLOW_PIN_DESCRIPTION" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp" >
<ImageView
android:id="@+id/greenPinImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/green_pin"
android:contentDescription="@string/A_MORE_GREEN_PIN_IMAGE" />
<TextView
android:id="@+id/greenPinText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/greenPinImage"
android:text="@string/A_MORE_GREEN_PIN_DESCRIPTION" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/thanksText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/A_MORE_CREATED_BY_TEXT" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
Edit: I can't remember exactly what I changed but I was able to get it to work with this xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="55dp" >
<TextView
android:id="@+id/mainDescText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="3"
android:scrollHorizontally="false"
android:text="@string/A_MORE_DESCRIPTION_TEXT" />
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp" >
<TextView
android:id="@+id/ageCurrentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ageButton"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="@string/A_MORE_CURRENT_SIGHTINGS_AGE" />
<Button
android:id="@+id/ageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:onClick="loadSightingsAge"
android:text="@string/A_MORE_SIGHTINGS_AGE_BUTTON" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/languageCurrentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/languageButton"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="@string/A_MORE_CURRENT_LANGUAGE" />
<Button
android:id="@+id/languageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:onClick="loadLanguage"
android:text="@string/A_MORE_LANGUAGE_BUTTON" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp" >
<ImageView
android:id="@+id/redPinImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/red_pin"
android:contentDescription="@string/A_MORE_RED_PIN_IMAGE" />
<TextView
android:id="@+id/redPinText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/redPinImage"
android:lines="3"
android:scrollHorizontally="false"
android:text="@string/A_MORE_RED_PIN_DESCRIPTION" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp" >
<ImageView
android:id="@+id/yellowPinImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/yellow_pin"
android:contentDescription="@string/A_MORE_YELLOW_PIN_IMAGE" />
<TextView
android:id="@+id/yellowPinText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/yellowPinImage"
android:lines="3"
android:scrollHorizontally="false"
android:text="@string/A_MORE_YELLOW_PIN_DESCRIPTION" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp" >
<ImageView
android:id="@+id/greenPinImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/green_pin"
android:contentDescription="@string/A_MORE_GREEN_PIN_IMAGE" />
<TextView
android:id="@+id/greenPinText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/greenPinImage"
android:lines="3"
android:scrollHorizontally="false"
android:text="@string/A_MORE_GREEN_PIN_DESCRIPTION" />
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/thanksText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center"
android:lines="3"
android:scrollHorizontally="false"
android:text="@string/A_MORE_CREATED_BY_TEXT" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
回答1:
I had the same exact problem. Let me just say that i just recently changed from using the Android 2.3.3 Platform up to Android 4.0.3 platform cause I wanted to take advantage of the Holo themes. So my default theme looked like this.
<style name="mytheme" parent="android:Theme.Holo">
<item name="android:windowNoTitle">true</item>
</style>
All my textViews looked great on my phone running Android 4.0.3
But when I ran my app in an emulator running Android 2.3.3 none of the textViews wrapped. I googled and googled and pretty much tried every combination of:
android:inputType="textMultiLine"
android:scrollHorizontally="false"
android:ellipsize="none"
android:layout_weight="1"
Nada. So then I decided to revert to my original theme:
<style name="myapp" parent="android:Theme.Black.NoTitleBar"/>
Viola! TextViews started wrapping again. Go figure. Not a real solution but maybe someone else can shed some light on why.
回答2:
Here's a good related answer showing that you can use a different theme depending on the version of Android it is running on - just create a versioned styles folder.
Answer here: TextView won't break text
回答3:
I had a similar problem regarding Android 2.3 and 4.x. As cited by @Richard Le Mesurier in an answer to this question, the issue is related to the theme: Android 4.x uses the Holo theme, which doesn't break long lines. The solution, as cited by Richard, is achieved by using versioned styles.
But I've solved the problem without using versioned styles by adding two properties to the EditText: android:scrollHorizontally="false" and android:inputType="textMultiLine". The problem is by adding inputType as textMultiline, the spell checker is enabled in Android 4.x. So, by adding "textNoSuggestions" to the inputType property, the spellchecker is disabled and the problem is solved.
<TextView
android:id="@+id/my_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:inputType="textMultiLine|textNoSuggestions"
android:layout_marginTop="2dp"
android:ellipsize="end" />
来源:https://stackoverflow.com/questions/11136145/android-textview-text-not-wrapping-within-relativelayout-on-version-2-3-3