【问题标题】:Unable to select TextView inside a Fragment无法在 Fragment 中选择 TextView
【发布时间】:2018-12-31 00:03:16
【问题描述】:

我正在尝试从片段内的 TextView 中选择文本。 这是我的 TextView 的 XML。 textIsSelectable、focusable、enabled 和 longclickable 属性都根据this post 设置为true。

                <TextView
                    android:id="@+id/post_text_recycle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/post_divider"
                    android:layout_marginEnd="16dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:autoLink="web"
                    android:fontFamily="serif"
                    android:text="@string/stall_user"
                    android:textColor="@android:color/black"
                    android:textColorHighlight="@android:color/holo_blue_light"
                    android:textIsSelectable="true"
                    android:focusable="true"
                    android:enabled="true"
                    android:longClickable="true"
                    android:textSize="16sp" />

我还在 Fragment 活动中以编程方式设置了以下内容:

text.setTextIsSelectable(true);

如果有帮助,我将从通过包含片段的 Activity 传递的 Bundle 中获取数据,然后我将使用以下方法设置 TextView:

text.setText(Html.fromHtml(data.getString("text")));
text.setTextIsSelectable(true);

我仍然无法选择文本。我在一些 Stack Overflow 帖子中读到,将宽度/高度设置为“wrap_content”允许您选择文本(我猜是一些旧的 Android 错误)。这个技巧在另一个活动中对我的 recyclerview TextView 有效。在这里好像不行。

感谢您的帮助!

编辑: 以下是请求的整个 Fragment 布局代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapp.www.ViewFragments.OriginalPostFragment">

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

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

            <TextView
                android:id="@+id/hide_toggle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:text=""
                android:layout_alignParentTop="true"
                android:background="#eeeeee"
                android:gravity="end"
                android:padding="4dp"
                android:layout_marginEnd="16dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:elevation="6dp"
                />

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/detail_wrapper"
                android:background="#ffffff"
                android:descendantFocusability="blocksDescendants"
                android:paddingBottom="48dp"
                cardview:cardCornerRadius="4dp"
                cardview:cardElevation="4dp">

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

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/user_image_recycle"
                        android:layout_width="60dp"
                        android:layout_height="60dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp" />

                    <TextView
                        android:id="@+id/post_type_recycle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:layout_toRightOf="@+id/user_image_recycle"
                        android:textStyle="italic" />

                    <TextView
                        android:id="@+id/post_title_recycle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/post_type_recycle"
                        android:layout_marginStart="16dp"
                        android:layout_toRightOf="@+id/user_image_recycle"
                        android:maxLines="1"
                        android:textColor="@android:color/black"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/user_name_recycle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/post_title_recycle"
                        android:layout_marginStart="16dp"
                        android:layout_toEndOf="@+id/user_image_recycle"
                        android:textColor="#0094BD"
                        android:textSize="16sp" />

                    <ImageView
                        android:id="@+id/post_divider"
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:layout_below="@+id/user_image_recycle"
                        android:layout_marginEnd="16dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:background="@android:color/black" />

                    <TextView
                        android:id="@+id/post_text_recycle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/post_divider"
                        android:layout_marginEnd="16dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:autoLink="web"
                        android:fontFamily="serif"
                        android:text="@string/stall_user"
                        android:textColor="@android:color/black"
                        android:textColorHighlight="@android:color/holo_blue_light"
                        android:textIsSelectable="true"
                        android:focusable="true"
                        android:enabled="true"
                        android:longClickable="true"
                        android:textSize="16sp" />

                </RelativeLayout>
            </android.support.v7.widget.CardView>

        </RelativeLayout>
    </ScrollView>

</FrameLayout>

【问题讨论】:

  • 你也可以添加片段布局吗?
  • @Signo 现在检查。我已经添加了整个片段代码
  • 太多的嵌套视图层次结构由于性能原因不太好,请参阅链接developer.android.com/topic/performance/rendering/…
  • @nyulan 感谢您提供此链接。我会尽量减少层次结构。你认为问题是因为层次结构吗?

标签: android android-fragments textview


【解决方案1】:

原因是descendantFocusability。在您的CardView 布局中,您将descendantFocusability 设置为blocksDescendants。 它的作用是禁用后代的可聚焦性,因此您的 TextView 永远不会获得这些焦点事件。 欲了解更多信息,请参阅here

要使其正常工作,只需从 CardView 中删除这一行

android:descendantFocusability="blocksDescendants"

【讨论】:

  • 谢谢。有效。我在另一个地方阻止了后代,因为视图会自动向下滚动。这里没有这样做,所以我想如果我删除它应该没问题。再次感谢!
  • 嗨,我有同样的问题,但我的 xml 中没有任何 android:descendantFocusability="blocksDescendants"。 Keep不明白为什么我不能选择
猜你喜欢
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多