实现方案:
首先,SeekBar布局如下,不直接调整SeekBar的尺寸,而是为其添加一个包裹的FrameLayout作为透明覆盖层。
<FrameLayout
android:id="@+id/touch_overlay"
android:layout_width="wrap_content" <!-- 保持与SeekBar一致 -->
android:layout_height="wrap_content"
android:paddingTop="16dp" <!-- 顶部额外触摸区域 -->
android:paddingBottom="16dp" <!-- 底部额外触摸区域 -->>
<androidx.appcompat.widget.AppCompatSeekBar
android:id="@+id/seek_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"/>
</FrameLayout>
Activity或Fragment中,为这个透明覆盖层设置触摸监听器,并确保触摸事件正确地传递给SeekBar。
FrameLayout touchOverlay = findViewById(R.id.touch_overlay);
AppCompatSeekBar seekBar = findViewById(R.id.seek_bar);
touchOverlay.setOnTouchListener((v, event) -> {
// 直接传递事件给SeekBar,因为我们没有调整SeekBar的大小,所以不需要偏移MotionEvent
return seekBar.onTouchEvent(event);
});