在 textView 中使用 textIsSelectable

using textIsSelectable in textView

我对 TextView 使用 android:textIsSelectable="true"

启用copy/select/paste对于TextView可以

问题

当我打开这个 activity 时,它会自动滚动到中间,像这样 Link

为什么会出现这个问题?

这是我的布局代码。

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

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:bind="http://schemas.android.com/apk/res/android" >

    <data>
            <variable
                name="aboutMbti"
                type="mbtitest.kiars.me.mbti.model.AboutMbti"/>  
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardBackgroundColor="@android:color/white"
            app:cardCornerRadius="2dp"
            app:cardElevation="5dp" >

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:fitsSystemWindows="true"
                android:scrollbarSize="1dp"
                android:scrollbarThumbVertical="@android:color/white">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/img_splash_mtbi"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:layout_margin="3dp"
                        android:adjustViewBounds="true"
                        android:padding="5dp"
                        android:scaleType="centerCrop"
                        bind:imageUrl="@{aboutMbti.imageUrl}"/>

                    <TextView
                        android:id="@+id/txt_content_about_mtbi"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/img_splash_mtbi"
                        android:layout_margin="5dp"
                        android:textIsSelectable="true"
                        android:padding="@dimen/title_padding"
                        android:text="@{aboutMbti.textContent}"
                        android:textColor="@color/ques_title"
                        android:textSize="15sp"
                        android:textStyle="bold"/>

                </RelativeLayout>

            </androidx.core.widget.NestedScrollView>

        </androidx.cardview.widget.CardView>

    </RelativeLayout>

</layout>

解决方案

您可以将 android:focusableInTouchMode="true"android:descendantFocusability="blocksDescendants" 添加到 NestedScrollView

中的子布局

在您的布局中尝试以下代码:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ...... >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true" >

    ...........

最后,根据https://whosebug.com/users/6891563/khemraj评论,我找到了

我的解决方案...

只需将 android:focusableInTouchMode="true" 添加到 ImageView,如下所示:

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

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:bind="http://schemas.android.com/apk/res/android" >

    <data>
            <variable
                name="aboutMbti"
                type="mbtitest.kiars.me.mbti.model.AboutMbti"/>  
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardBackgroundColor="@android:color/white"
            app:cardCornerRadius="2dp"
            app:cardElevation="5dp" >

            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:fitsSystemWindows="true"
                android:scrollbarSize="1dp"
                android:scrollbarThumbVertical="@android:color/white">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/img_splash_mtbi"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:layout_margin="3dp"
                        android:adjustViewBounds="true"
                        android:padding="5dp"
android:focusableInTouchMode="true"
                        android:scaleType="centerCrop"
                        bind:imageUrl="@{aboutMbti.imageUrl}"/>

                    <TextView
                        android:id="@+id/txt_content_about_mtbi"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/img_splash_mtbi"
                        android:layout_margin="5dp"
                        android:textIsSelectable="true"
                        android:padding="@dimen/title_padding"
                        android:text="@{aboutMbti.textContent}"
                        android:textColor="@color/ques_title"
                        android:textSize="15sp"
                        android:textStyle="bold"/>

                </RelativeLayout>

            </androidx.core.widget.NestedScrollView>

        </androidx.cardview.widget.CardView>

    </RelativeLayout>

</layout>

Tnx 伙计们