我在我的布局中使用了一个 SeekBar 并且我有另一个 View 是一个 RelativeLayout。
<CoordinatorLayout>
<RelativeLayout>
...
</RelativeLayout>
<SeekBar
android:id="@+id/progressSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="70"
android:progressDrawable="@drawable/player_seek_bar_progress"/>
</CoordinatorLayout>
当我触摸这个 RelativeLayout 时,SeekBar 获得了焦点...... 即使在我的代码中,我也没有发现我的布局有任何问题...
有人遇到同样的问题吗?
谢谢你们的回答:)
最佳答案
我刚刚遇到了完全相同的问题。
我的布局类似于以下内容(我省略了与该主题无关的 View )。如您所见,父 View (LinearLayout
)和 SeekBar
都有 android:clickable="true"
属性。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" />
</LinearLayout>
如何解决? :]
这是我在 #android.view.ViewGroup#dispatchSetPressed
的源代码中找到的内容(这是每次用户触摸屏幕时都会调用的事件)
它遍历每个 subview ,如果满足以下条件,它会将事件传播到该 subview :
if (!pressed || (!child.isClickable() && !child.isLongClickable())) {
child.setPressed(pressed);
}
基本上,如果您的 subview (在这种情况下,SeekBar
是可点击的或可长时间点击的,您将不会将事件传播给它。
此外,还有一条评论如下:
Children that are clickable on their own should not show a pressed state when their parent view does. Clearing a pressed state always propagates.
此评论描述的情况与您现在面临的情况相同。如果您将 SeekBar
设置为可点击,Android 知道必须直接在 SeekBar
中对点击事件使用react,并且它不会传播该事件。
这对我有用。希望它也对您有用。
引用
关于android - 不需要 Seekbar 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39622270/