android - textIsSelectable 不适用于 descendantFocusability=blocksDescendants

标签 android android-layout textview android-relativelayout

我遇到了一个问题,即 fragment 中的 ScrollView 自动滚动到底部。因此,为了解决此问题,我按照 this 中的建议在 RelativeLayout 中添加了以下行回答:

android:descendantFocusability="blocksDescendants"

但添加此行后,textIsSelectable 停止工作。如何添加这两行?

这是 XML:

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

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp"
        android:textSize="23sp"
        android:text="The Beach Beneath the Street"
        android:fontFamily="sans-serif-medium"
        android:textColor="@color/black"
        android:textIsSelectable="true"/>

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:padding="15dp"
        android:fontFamily="sans-serif-condensed"
        android:lineHeight="25dp"
        android:lineSpacingMultiplier="1"
        android:lineSpacingExtra="6dp"
        android:textSize="16sp"
        android:text="Never add a RecyclerView or ListView to a scroll view"
        android:textIsSelectable="true" />

</RelativeLayout>

最佳答案

当您在 ViewGroup 中使用 android:descendantFocusability="blocksDescendants" 时,它将阻止其所有 subview 接收焦点

更多信息请查看文档

Must be one of the following constant values in android:descendantFocusability.

  1. afterDescendants:- ViewGroup 只有在其后代都不需要时才会获得焦点。
  2. beforeDescendants:- ViewGroup 将在其任何后代之前获得焦点。
  3. blocksDescendants:- ViewGroup 将阻止其后代获得焦点。

现在你的问题

But after adding this line, textIsSelectable is stopped working for. How can I add both lines?

AFAIK你不能同时使用两者

您需要为 fragment 中自动滚动到底部的问题找到替代解决方案

这里有一些链接可能会帮助您解决 fragment 中自动滚动到底部的问题

关于android - textIsSelectable 不适用于 descendantFocusability=blocksDescendants,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54097016/

相关文章:

Android:自定义幻灯片布局

android - 在android中自动更改壁纸

javascript - 如何在一种方法中触发多个 View 元素(React Native JS)

android - 布局占用超过预期

android - 从 TextView 获取文本

android - 无法在 ClipboardManager primaryClip 重新分配 Val

java - 从自定义Listview中获取所有ImageView

Android Layout 设计求助!

android TextView setText 不工作

android - TextView 在 ListView 下面,但是在 parent 的底部,为什么?