动态填充的TextView截断了最后一行文本

5
我不确定是“最后一行”的问题,但我们有一个应用程序,其中有一个TextView,宽度为fill_parent,高度为wrap_content。文本从Java代码中动态放置在其中。即使布局中有足够的空间,文本的最后一行也没有显示出来。它在相当深的视图层次结构中,因此我的猜测是那里的测量逻辑被搞乱了,但这非常令人沮丧。我们需要猜测文本中将有多少行,并相应地设置'android:lines'才能使其正常工作。
有人见过这种情况吗?在代码中,向下看到id 'contentTextView'。如果我去掉'android:lines',文本的最后一行会消失。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/flag"
        >

    <include android:id="@+id/incHeader" layout="@layout/header"/>

    <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">

            <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="101dp"
                    android:background="@drawable/headershadow"/>

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="invisible"
                    android:layout_weight="1"/>


            <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="101dp"
                    android:background="@drawable/footershadow"/>

        </LinearLayout>

        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/gybcontentback"
                android:layout_gravity="center_horizontal"
                android:orientation="vertical">

            <include android:id="@+id/gybHeaderInclude" layout="@layout/gybcontentheader"/>


                <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:textColor="#4f4f4f"
                        android:paddingLeft="15dp"
                        android:paddingRight="15dp"
                        android:lines="9"
                        android:id="@+id/contentTextView"/>

            <ImageView
                    android:layout_marginTop="15dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/button_blue_rascal_button"
                    android:id="@+id/buttonMoreInfo"/>

        </LinearLayout>

    </FrameLayout>


    <include android:id="@+id/incFooter" layout="@layout/menu"/>

</LinearLayout>

Java 代码。我在 onCreate 方法中使用普通的 Java 字符串填充 TextView。

@Override
protected String body()
{
    return "Rascal Flatts and The Jason Foundation, Inc. are working together to prevent suicide.\n\n" +
            "Your Battle Buddy (or family member) may need a friend.\n\n" +
            "Take the pledge to B1.";
}

基类调用此方法以获得实际文本。
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(contentView());
    wireMenu();

    TextView headerText = (TextView) findViewById(R.id.gybHeaderText);
    if(headerText != null)
        headerText.setText(header());

    ((TextView) findViewById(R.id.contentTextView)).setText(body()); // This is where the text is set
}

无论如何,我将其简化。我去掉了一些部分以查看剩下的部分,但仍然遇到了同样的问题。我认为我可能已经找到了引发问题的因素和解决方案,但不是“原因”。

我们能看到与该问题相关的Java代码吗? - Jarno Argillander
凯文,看一下你调用setText()的时候可能会有帮助,因为那可能会影响wrap_content实际绘制的方式。 - LuxuryMode
7个回答

8

我有同样的问题。我找到了一个解决方法。在你的文本末尾添加一个新行 "\n"。这样将始终显示你所有的文本。


1
这是有效的,唯一的问题在于当文本只有一行时,会在其下面留下一个空白行。 - Dave Feldman
这对我来说是最直接和最简单的解决方案。 - u2tall
尝试了无数其他的解决方案后,这个简单的加法方法非常有效! - Matt M
现在这显示了所有我的文本,但是视图的测量高度似乎存在不一致。 - Howard Swope

2

你尝试过直接将TextView与一个LinearLayout包围,将宽度和高度设置为wrap_content吗?

这对我很有效...


2

我将布局简化为其基本部分。这是削减了的版本,仍然截断了文本:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/flag"
        >


    <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:orientation="vertical">

            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#4f4f4f"
                    android:id="@+id/contentTextView"/>

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/button_blue_rascal_button"
                    android:id="@+id/buttonMoreInfo"/>

        </LinearLayout>



</LinearLayout>

我认为问题出在父LinearLayout从其中的ImageView中获取其宽度。在这一混乱中,TextView获取了其请求高度的错误值。我想这可能与UI计算有关。父级必须询问其子级的请求宽度,然后返回并告诉每个人他们的宽度是多少,然后再请求高度。类似于这样的过程。无论如何,以下方法可行。图像宽度为263像素(在mdpi中),因此如果我在父布局上设置263dp而不是wrap_content,一切都会正常。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/flag"
        >


    <LinearLayout
                android:layout_width="263dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:orientation="vertical">

            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#4f4f4f"
                    android:id="@+id/contentTextView"/>

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/button_blue_rascal_button"
                    android:id="@+id/buttonMoreInfo"/>

        </LinearLayout>



</LinearLayout>

将TextView设置为wrap_content也可以,但它会将整个布局推得比我想要的要大。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/flag"
        >


    <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:orientation="vertical">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#4f4f4f"
                    android:id="@+id/contentTextView"/>

            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/button_blue_rascal_button"
                    android:id="@+id/buttonMoreInfo"/>

        </LinearLayout>



</LinearLayout>

不确定这里的要点是什么。简而言之,如果您开始看到计算高度问题,请考虑为父级元素设置一个宽度,以便UI计算不会混淆。


如果您能提供更详细的答案,即使我“回答”了自己的问题,我也会投票支持您的答案 ;) - Kevin Galligan
这里有一个很好的答案,帮助我解决了这个问题。 - CodeMonkey

2

我有一个不同的问题,但与此类似。下面是我认为适用于您所需的解决方案。您可以在任何类型的ViewGroup中使用全局布局监听器来监听TextView。

    final TextView dSTextView = (TextView)findViewById(R.id.annoyingTextView);
dSTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        dSTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

        float lineHeight = dSTextView.getLineHeight();
        int maxLines = (int) (dSTextView.getHeight() / lineHeight);

        if (dSTextView.getLineCount() != maxLines) {
            dSTextView.setLines(maxLines);
        }

    }
});

您可以在这里阅读更多关于它的内容:链接

0
尝试在您的TextView中添加android:padding。对我有作用。

0
将有问题的TextView放在FrameLayout(或某个容器)中。我认为问题是由于你的兄弟视图引起的。

0

你试过了吗?

android:singleLine="false" 

我会试一试,但它显示的不止一行。只是不显示最后一行。 - Kevin Galligan

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