79

誰かがテキストの何が問題になっているのか教えてもらえますか?1行より長いテキストは次の行に折り返されませんが、画面を超えます。

コードは次のとおりです。

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

24 に答える 24

39

この質問に対する唯一の正しい答えは、親を適切な幅 (この場合FILL_PARENTWRAP_CONTENT)に設定android:layout_weight=1し、ラップする必要があるテキストビューに使用する必要があるということです。

SingleLineはデフォルトでオンになっているため、変更は行われません。

0pxwidthに設定しても機能しますが、適切な解決策ではありません。

xml を使用したいくつかの例 (今回はテーブルビュー):

<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 を 3 番目のパラメーターとして探しています。

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));
于 2011-08-31T09:20:15.263 に答える
20

2 つのパラメーターを使用する必要があります。

  • android:ellipsize="none": テキストはテキスト ビューの幅で切り取られません

  • android:scrollHorizontally="false"テキストは必要な行数で折り返されます

于 2011-03-02T14:21:41.640 に答える
14

xml ファイルで使用するだけで十分です。

android:singleLine="false".

それがうまくいくことを願っています。

ではごきげんよう!

于 2013-05-21T09:29:53.037 に答える
11

を使用すると、これらのソリューションのいずれも機能しませんでしたTableLayout>TableRow>TextView。それから私は見つけましTableLayout.shrinkColumns="*"た。機能した他の唯一の解決策は、TextView をlayout_width:250pxetc に強制することでしたが、そのような幅を強制するのは好きではありません。

テーブルを操作する場合は、このようなことを試してください。

            <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

それが誰かを助けることを願っています。

于 2011-06-24T03:35:29.073 に答える
4

コード内のレイアウト パラメータの 1 つが間違っています。最初の TextView で

android:layout_width="wrap_content"

への変更

android:layout_width="fill_parent" 

画面幅のサイズを超えるテキストは、次の行に折り返されて設定されandroid:singleline="false"ます。

于 2010-02-25T06:29:19.070 に答える
3

テキスト ビューandroid:minHeight="some pixes"またはの高さを設定しますandroid:width="some pixels"。それは問題を解決します。

于 2011-03-21T13:55:45.467 に答える
1

私は Android (および GUI) の初心者ですが、ソフトウェアの経験は豊富です。私はいくつかのチュートリアルを経験しましたが、これが私の理解です:

layout_width と layout_height は TextView の属性です。これらは、TextView がそれ自体を形成する方法についての指示であり、TextView 内のコンテンツを処理する方法については言及していません。

「fill_parent」を使用する場合は、TextView がその親ビューに対して相対的に形成され、それを埋める必要があると言っています。

「wrap_content」を使用する場合は、親ビューを無視し、TextView の内容にその形状を定義させる必要があると言っています。

ここがややこしいポイントだと思います。「wrap_content」は、TextView に内容を管理する方法 (行を折り返す方法) を指示するのではなく、内容に応じて形を整える必要があることを伝えています。この場合、改行文字がないため、すべてのテキストが 1 行に収まるように整形されます (残念ながら、親をオーバーフローしています)。

親を水平に塗りつぶし、内容を垂直に折り返す必要があると思います。

于 2011-12-18T18:45:21.070 に答える
1

高さを増やします。android:height="Some size" 、それは私にとってはうまくいっています。

于 2014-06-09T13:32:07.440 に答える
1

これは古いスレッドですが、私の経験を共有したいと思います。私のアプリケーションは OS 2.0 および 4.0+ では正常に動作していましたが、OS 3.x を実行している HTC 電話ではテキストが折り返されませんでした。私にとってうまくいったのは、これらのタグの両方を含めることでした。

android:maxLines="100"
android:scrollHorizontally="false"

どちらかを削除すると、os 3.0 デバイスだけでは機能しませんでした。「ellipsize」パラメーターにはニュートラルな効果がありました。これが以下の完全なテキストビュータグです

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

これが誰かを助けることを願っています。

于 2013-03-15T07:45:06.147 に答える
0

ディスプレイのレイアウトの特定の組み合わせに依存すると思います。一部のフラグは上書きまたは無視される場合があります。タブ付きのTabHostがあり、各タブはテーブルのリストです。つまり、これはListViewのタブであり、各行はTextViewのTableLayoutです。上記の修正を試しましたが、どれも機能しませんでした。

于 2010-08-26T16:31:54.410 に答える
0

この問題を解決するために、最終的に TextView の高さにいくつかのピクセルを追加することができました。

まず、TextView の高さを実際に取得する必要があります。塗装前は0なので一筋縄ではいきません。

このコードを 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);
            }
        }
    });
}

この行をdimens.xmlに追加する必要があります

<dimen name="view_add_height">10dp</dimen>

それが役に立てば幸い。

于 2016-04-04T10:36:32.967 に答える
0

を削除android:lines="1"して追加android:maxLines="2"したところ、テキストが自動的に折り返されました。問題はandroid:lines属性でした。これにより、テキストの折り返しが発生しません。

maxEmsこれを修正するためにor singleLine="false"(非推奨の API) を使用する必要はありませんでした。

于 2016-06-27T10:08:03.770 に答える
0

android:singleLine="false"urTextViewタグで使用する必要があります。

于 2010-02-16T18:19:16.513 に答える
0

私の場合、ネストを使用して、 on 、およびon andに設定することでTableRow > ScrollView > TextView問題を解決しました。android:layout_widthfill_parentTableRowwrap_contentScrollViewTextView

于 2015-05-04T04:56:47.107 に答える
-5

テキストを折り返して次の行に配置するには、レイアウト xml ファイルで「\n」つまり改行文字を使用し、レイアウト画面ではなくエミュレータで変更を確認する必要があります。

于 2011-08-11T10:20:23.527 に答える