expandableTextView中的复制功能在更改方向时会产生错误

时间:2017-09-10 06:49:24

标签: android android-fragments textview

我在片段中创建了一些expandableTextView而没有启用复制功能。即使我改变方向,这也能正常工作。

但是当我在xml TextView中给出android:textIsSelectable="true"时,我面临的问题是:

  • 更改方向时,所有TextView的内容都会根据上一个TextView的内容进行更改。 我可以知道为什么会这样,我该怎么做才能解决它?

fragment_layout.xml:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dddddd">

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <include
                android:id="@+id/sample1"
                layout="@layout/text_item" />

            <include
                android:id="@+id/sample2"
                layout="@layout/text_item" />

            <include
                android:id="@+id/sample3"
                layout="@layout/text_item" />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

text_item.xml:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:expandableTextView="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:gravity="bottom"
        android:singleLine="true"
        android:textColor="#333333"
        android:textSize="18sp"
        android:padding="5dp"
        android:background="@color/title_bg_color"

        />

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expand_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        expandableTextView:maxCollapsedLines="2">

        <TextView
            android:id="@id/expandable_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:fontFamily="sans-serif-light"
            android:textSize="16sp"
            android:textColor="#494848"
            android:padding="5dp"
            android:background="@color/text_bg_color"
            android:textIsSelectable="true"
            />
        <ImageButton
            android:id="@id/expand_collapse"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:padding="16dp"
            android:layout_gravity="right|bottom"
            android:background="@color/text_bg_color"
            /> <!--android:background="@android:color/transparent"-->
    </com.ms.square.android.expandabletextview.ExpandableTextView>

</LinearLayout>

activity.java:

 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);

    ((TextView) rootView.findViewById(R.id.sample1).findViewById(R.id.title)).setText(Html.fromHtml("<font color=#ffffff> A</font>"));
    ((TextView) rootView.findViewById(R.id.sample2).findViewById(R.id.title)).setText(Html.fromHtml("<font color=#ffffff> B</font>"));
    ((TextView) rootView.findViewById(R.id.sample3).findViewById(R.id.title)).setText(Html.fromHtml("<font color=#ffffff> C</font>"));

    ExpandableTextView expTv1 = (ExpandableTextView) rootView.findViewById(R.id.sample1)
            .findViewById(R.id.expand_text_view);

    ExpandableTextView expTv2 = (ExpandableTextView) rootView.findViewById(R.id.sample2)
            .findViewById(R.id.expand_text_view);

    ExpandableTextView expTv3 = (ExpandableTextView) rootView.findViewById(R.id.sample3)
            .findViewById(R.id.expand_text_view);

    expTv1.setText(Html.fromHtml(getString(R.string.A)));
    expTv2.setText(Html.fromHtml(getString(R.string.B)));
    expTv3.setText(Html.fromHtml(getString(R.string.C)));

    return rootView;
}

启用复制时的屏幕截图: Contents changed in landscapecontents changed in portrait

0 个答案:

没有答案