简体   繁体   English

Android TextView文本未包含在2.3.3版本的RelativeLayout中

[英]Android TextView text not wrapping within RelativeLayout on version 2.3.3

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. 我一直在尝试使用TextView将文本包装到Android 2.3.3上的多行中一段时间​​,但我无法接通以使其工作,即使在最基本的级别上也是如此。 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. 它在Android 4上运行得很好,但我也希望以2.3.3为目标。

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:

<?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>

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. 我只想说我刚刚使用Android 2.3.3平台更改为Android 4.0.3平台,因为我想利用Holo主题。 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 我的所有textViews在运行Android 4.0.3的手机上看起来都很棒

But when I ran my app in an emulator running Android 2.3.3 none of the textViews wrapped. 但是当我在运行Android 2.3.3的模拟器中运行我的应用程序时,没有任何textViews包装。 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. TextViews再次开始包装。 Go figure. 去搞清楚。 Not a real solution but maybe someone else can shed some light on why. 不是一个真正的解决方案,但也许其他人可以解释为什么。

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. 这是一个很好的相关答案,表明您可以根据其运行的Android版本使用不同的主题 - 只需创建一个版本化的样式文件夹。

Answer here: TextView won't break text 在这里回答: TextView不会破坏文本

I had a similar problem regarding Android 2.3 and 4.x. 关于Android 2.3和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. 正如@Richard Le Mesurier在回答这个问题时引用的那样,问题与主题有关:Android 4.x使用Holo主题,它不会打破长线。 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". 但是我通过向EditText添加两个属性来解决问题而不使用版本化样式:android:scrollHorizo​​ntally =“false”和android:inputType =“textMultiLine”。 The problem is by adding inputType as textMultiline, the spell checker is enabled in Android 4.x. 问题是通过将inputType添加为textMultiline,Android 4.x中启用了拼写检查程序。 So, by adding "textNoSuggestions" to the inputType property, the spellchecker is disabled and the problem is solved. 因此,通过向inputType属性添加“textNoSuggestions”,将禁用拼写检查程序并解决问题。

<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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM