无法在片段内选择TextView



我正试图从片段中的TextView中选择文本。这是我的TextView的XML。根据这篇文章,textIsSelectable、focusable、enabled和longclickable属性都设置为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);

如果有帮助的话,我将从通过保存片段的活动传递的Bundle中获取数据,然后使用设置TextView

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

我仍然无法选择文本。我在Stack Overflow的一些帖子中读到,将width/height设置为"wrap_content"可以让你选择文本(我想是一些旧的Android bug(。这个技巧在另一个活动中对我的recyclerview TextView起到了作用。在这里似乎不起作用。

谢谢你的帮助!

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

<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>

原因是descendantFocusability。在CardView布局中,将descendantFocusability设置为blocksDescendants。它的作用是禁用子体的可聚焦性,因此您的TextView永远不会获得这些聚焦事件。有关更多信息,请参阅此处。

要使其工作,只需从CardView中删除此行

android:descendantFocusability="blocksDescendants"

最新更新