首页 > 解决方案 > 当文本很大并且正在滚动时,TextView Shape 背景不显示

问题描述

这是问题场景:

我有一个,Activity包括一个ScrollView, 小时候ScrollView只有一个,而有一个。XML 文件在这里:LinearLayoutLinearLayoutTextView

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bubble_shape"
                android:text="@string/TestLargeText"/>
        </LinearLayout>

    </HorizontalScrollView>

</LinearLayout>

“bubble_shape”drawable在这里:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <corners
        android:bottomLeftRadius="15dp"
        android:bottomRightRadius="15dp"
        android:topLeftRadius="15dp"
        android:topRightRadius="15dp"/>
    <padding
        android:bottom="12dp"
        android:left="12dp"
        android:right="12dp"
        android:top="12dp"/>
    <stroke
        android:width="8dp"
        android:color="#ff00ff"/>
</shape>

我有一个大文本,TextView这里是问题:当TextView有大文本时,背景形状没有显示。您可以在下面的图片中看到问题:

xml文件宽度小文本

和相同的xml宽度大文本!

我该如何解决这个问题?

标签: androidtextviewscrollviewshapes

解决方案


我用两种方式解决了你的问题。

1-将“bubble_shape”设置为您的HorizontalScrollView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bubble_shape">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/TestLargeText" />
        </LinearLayout>

    </HorizontalScrollView>

</LinearLayout>

2-或只是更改“bubble_shape”中的角标签,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="15dp" />        

    <padding
        android:bottom="12dp"
        android:left="12dp"
        android:right="12dp"
        android:top="12dp" />


    <stroke
        android:width="8dp"
        android:color="#ff00ff" />
</shape>

推荐阅读