web-dev-qa-db-ja.com

AndroidでマルチラインTextView?

私は以下のようにしましたxml

<TableRow>
    <TextView Android:id="@+id/address1"
        Android:layout_width="fill_parent" 
        Android:layout_height="wrap_content"
        Android:gravity="left"
        Android:maxLines="4" 
        Android:singleLine="false"              
        Android:text="Johar Mor, Gulistan-e-Johar, Karachi" >
    </TextView> 
</TableRow>

multilineでは動作しません。TableLayout...を使用しています。

それで、私がここでしている間違いは何ですか?

140

TextViewに入れているテキストが短い場合は、自動的に4行に拡張されません。 TextView常ににするには、テキストの長さに関係なく4行にしたい場合は、Android:lines属性を設定します。

<TextView
    Android:id="@+id/address1"
    Android:gravity="left"
    Android:layout_height="fill_parent"
    Android:layout_width="wrap_content"
    Android:maxLines="4"
    Android:lines="4"
    Android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."></TextView>

あなたはTableRowでこれをすることができます、以下のコードを見てください

<TableRow >
        <TextView
            Android:id="@+id/tv_description_heading"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:gravity="left"
            Android:padding="8dp"
            Android:text="@string/rating_review"
            Android:textColor="@color/black"
            Android:textStyle="bold" />

        <TextView
            Android:id="@+id/tv_description"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:gravity="left"
            Android:maxLines="4"`enter code here`
            Android:padding="8dp"
            Android:text="The food test was very good."
            Android:textColor="@color/black"
            Android:textColorHint="@color/hint_text_color" />
    </TableRow>
183
Zarah

私はちょうどあなたが使用する場合は私はそれを追加すると思いました:

Android:inputType="textMultiLine"

その後、ビューはクリックできなくなります。スライドドロワーのメニューに複数行のテキストビューを表示しようとしていましたが、これは明らかにクリックに応答する必要がありました。

Android:singleLine="false"はうまくいきました。

103
Ryan

どちらの答えも好きではありません。 inputTypeを設定するだけでTextViewはその内容に適応します

<TextView
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:inputType="textMultiLine"/>

Nexus One(2.3)およびNexus 4(4.4)でテスト済み

28
Maragues

ScrollViewにテキストビューを追加するだけです

<ScrollView
           Android:layout_width="fill_parent"
           Android:layout_height="wrap_content"
           Android:layout_weight="1"
           Android:layout_marginLeft="15dp"
           Android:layout_marginRight="15dp"
           Android:layout_marginTop="20dp"
           Android:fillViewport="true">

           <TextView
               Android:id="@+id/txtquestion"
               Android:layout_width="fill_parent"
               Android:layout_height="match_parent"
               Android:background="@drawable/abs__dialog_full_holo_light"
               Android:lines="20"
               Android:scrollHorizontally="false"
               Android:scrollbars="vertical"
               Android:textSize="15sp" />

       </ScrollView>
10
Pravin Bhosale

単にテキストの中に '\ n'を入れてください。追加の属性は必要ありません:)

          <TextView
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:textAppearance="?android:attr/textAppearanceLarge"
            Android:text="Pigeon\nControl\nServices"
            Android:id="@+id/textView5"
            Android:layout_gravity="center"
            Android:paddingLeft="12dp"/> 
8
Naunidh Singh

私はテキストビューの行数を強要する解決策が好きではありません。私はむしろ解決策を使ってそれを解決することをお勧めします ここで提案 。私が見ているように、OPもテキストビューをテーブル内で適切に見せることに苦労しており、shrinkColumnsは必要なものを達成するために渡すべき正しいディレクティブです。

5
Boris Strandjev

EditTextをクリックできないようにしてフォーカスを合わせられないようにします。スクロールバーを表示してEditTextのアンダーバーを削除することもできます。
これは例です。

<EditText
Android:id="@+id/my_edit_text"
Android:layout_width="match_parent"
Android:layout_height="wrap_parent"
Android:inputType="textMultiLine"                   <!-- multiline -->
Android:clickable="false"                         <!-- unclickable -->
Android:focusable="false"                         <!-- unfocusable -->
Android:scrollbars="vertical"     <!-- enable scrolling vertically -->
Android:background="@Android:color/transparent"   <!-- hide the underbar of EditText -->
/>  

お役に立てれば :)

5
Khalil Meg

私が複数行のTextViewについて見つけた最良の答えは:

Android:inputType="textMultiLine"
3
Bhuvan Bhatt

まず"\n"を同等のHtml"&lt;br&gt;"に置き換えてから、文字列に対してHtml.fromHtml()を呼び出します。以下の手順に従ってください。

String text= model.getMessageBody().toString().replace("\n", "&lt;br&gt;")
textView.setText(Html.fromHtml(Html.fromHtml(text).toString()))

これは完璧に機能します。

3
Gulzar Bhat

あなたはAndroidを使用することができます:inputType = "textMultiLine"

  <TextView Android:id="@+id/address1"
    Android:layout_width="fill_parent" 
    Android:layout_height="wrap_content"
    Android:gravity="left"
    Android:maxLines="4" 
    Android:inputType="textMultiLine"
    Android:text="Johar Mor, Gulistan-e-Johar, Karachi" />
2
Enakhi

私が学んだことは、次の行に追加したい単語の間に「\ n」を追加することでした。例えば...

<TextView
    Android:id="@+id/time"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:textAlignment="center"
    Android:text="M-F 9am-5pm \n By Appointment Only" />

午後5時とByの間の\ nにより、次の行の開始位置と終了位置をより細かく制御できます。

2
Eduardo Lopez

私は使った:

TableLayout tablelayout = (TableLayout) view.findViewById(R.id.table);
tablelayout.setColumnShrinkable(1,true);

それは私のために働きました。 1は列数です。

1
You can do this with TableRow, see below code

<TableRow >
    <TextView
        Android:id="@+id/tv_description_heading"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:gravity="left"
        Android:padding="8dp"
        Android:text="@string/rating_review"
        Android:textColor="@color/black"
        Android:textStyle="bold" />

    <TextView
        Android:id="@+id/tv_description"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:gravity="left"
        Android:maxLines="4"`enter code here`
        Android:padding="8dp"
        Android:text="The food test was very good."
        Android:textColor="@color/black"
        Android:textColorHint="@color/hint_text_color" />
</TableRow>
0