Android:如何为TextView显示长文本

3

我需要在我的活动中显示一段长约250个字符的文本(单行,没有换行符或断行)。

我已经为布局视图使用了textMultiLine作为textType,并从活动中使用view.setText()方法传递了长文本。

但是,文本显示在可见屏幕之外。

如何解决?

谢谢, Nehatha

+++ 编辑 +++++

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:background="@drawable/bg" 
android:gravity="center_horizontal"
android:id="@+id/rootView">
<ScrollView android:id="@+id/ScrollView01"
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:layout_marginLeft="8dp" android:layout_marginRight="8dp"
    android:minWidth="310dp" android:scrollbarStyle="outsideOverlay"
    android:scrollbars="vertical">
        <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <FrameLayout android:id="@android:id/tabcontent"
                android:layout_width="fill_parent" android:layout_height="fill_parent">


                <LinearLayout android:id="@+id/tabContentPostDetails"
                    android:layout_width="wrap_content" android:orientation="vertical"
                    android:layout_height="fill_parent">

                    <TableLayout android:id="@+id/TableLayout01"
                        android:layout_width="fill_parent" android:layout_height="wrap_content"
                        android:gravity="center"
                        android:background="@drawable/transparent_border_shape">
                        <TableRow android:layout_marginBottom="5dip">
                            <LinearLayout android:orientation="horizontal"
                            android:layout_width="fill_parent" android:layout_height="wrap_content">
                                <TextView android:id="@+id/csPostTitle"
                                    android:padding="3dip" android:textSize="7pt"
                                    android:textColor="@color/Black"  
                                    android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textMultiLine"/>
                            </LinearLayout>
                        </TableRow>

                        ....

你最终找到了正确的答案吗? - Alexander Gorg
4个回答

2
在这种情况下,帮助我的是设置layout_gravity="fill_vertical",像这样:
    <TextView
        android:id="@+id/list_e_taskdesc"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="fill_vertical"
        android:text="@string/empty_label1"/>

2

您可能正在使用layout_width="wrap_content"。这会使TextView在单行中拉伸以容纳文本。尝试使用layout_width="fill_parent"(或给定宽度)和layout_height="wrap_content"


嗨,Gregory,我已经尝试使用“fill_parent”,但仍然无法看到整个文本。 - Venkat Papana
你能发布一下你的截图和布局吗? - Gregory
嗨,Gregory,我已经更新了我的原始帖子,并附上了我的布局代码片段,请检查@+id/csPostTitle视图。 - Venkat Papana

2

尝试使用 android:maxEms="15",我认为这会对你有帮助。


完美的解决方案!当将文本附加到同一TextView并且您想限制视图中任何给定行中的字母数量时。 - Avid Programmer

0

你需要为textview使用以下属性:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:maxLines="10"

将10更改为适合您的任何数字。

我测试的布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#ffffff"
    android:gravity="center_horizontal"
    android:id="@+id/rootView">
    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:minWidth="310dp"
        android:scrollbarStyle="outsideOverlay"
        android:scrollbars="vertical">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <LinearLayout
                    android:id="@+id/tabContentPostDetails"
                    android:layout_width="wrap_content"
                    android:orientation="vertical"
                    android:layout_height="fill_parent">

                    <TableLayout
                        android:id="@+id/TableLayout01"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:background="#ffffff">
                        <TableRow 
                            android:layout_marginBottom="5dip"
                            android:background="#ff0000"
                            >
                            <TextView
                                android:id="@+id/csPostTitle"
                                android:background="#00ff00"
                                android:padding="3dip"
                                android:textSize="7pt"
                                android:textColor="#000000"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:inputType="textMultiLine"
                                android:maxLines="10"
                                android:text="this is a very long text. so long that it does not ghave any breaks whasoever. this is a test to see if the line dissplayes proper;ly." />

                            <LinearLayout
                                android:orientation="horizontal"
                                android:layout_width="fill_parent"
                                android:layout_height="wrap_content">
                            </LinearLayout>
                        </TableRow>
                    </TableLayout>
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

嗨Mandel,这对我不起作用,文本仍然显示在一行中,我无法看到超出可见屏幕的文本。 - Venkat Papana
看看我测试过的布局。也许,有些其他的变化我忘了提到。将上面的布局与你的进行比较。 - Mandel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接