在 Android 上向下搜索时,Vertical SeekBar 上会出现一个透明点
我在我的 Android 应用程序中添加了一个垂直 SeekBar,它在 SeekBar 上不显示拇指。这一切正常,但有一个小问题 - 当我将 SeekBar 滚动/滑动到底部时,它开始在 SeekBar 顶部显示透明点。
SeekBar 是 LinearLayout 的一部分,它也是可滑动的(因为我们希望完整的 SeekBar 组件可滑动)。
我尝试了几件事,例如取消拇指、将 SeekBar 设置为不可聚焦等,但没有任何效果。
这是亮度控件的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/brightness_control_layout_parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/brightness_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mobile_vertical_control_background"
android:orientation="vertical"
android:paddingHorizontal="5dp"
android:paddingVertical="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingVertical="1dp"
android:text="+"
android:textColor="@color/white"
android:textSize="20sp" />
<com.example.view.wigdet.VerticalSeekBar
android:id="@+id/seekbar"
android:layout_width="10dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:progressTint="@color/white"
android:secondaryProgressTint="@color/light_grey"
android:splitTrack="false"
android:thumbTint="@android:color/transparent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingVertical="1dp"
android:text="-"
android:textColor="@color/white"
android:textSize="20sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/brightness_icon" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是垂直 SeekBar 视图:
class VerticalSeekBar : androidx.appcompat.widget.AppCompatSeekBar {
private var changeListener: OnSeekBarChangeListener? = null
constructor(context: Context?) : super(context!!) {}
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
context!!,
attrs,
defStyle
) {
}
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {}
override fun setOnSeekBarChangeListener(l: OnSeekBarChangeListener) {
this.changeListener = l
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(h, w, oldh, oldw)
}
@Synchronized
override fun setProgress(progress: Int) // it is necessary for calling setProgress on click of a button
{
super.setProgress(progress)
onSizeChanged(width, height, 0, 0)
}
@Synchronized
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec)
setMeasuredDimension(measuredHeight, measuredWidth)
}
override fun onDraw(c: Canvas) {
c.rotate(-90f)
c.translate(-height.toFloat(), 0f)
super.onDraw(c)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
if (!isEnabled) {
return false
}
when (event.action) {
MotionEvent.ACTION_DOWN -> {
isSelected = true
isPressed = true
changeListener?.onStartTrackingTouch(this)
}
MotionEvent.ACTION_UP -> {
isSelected = false
isPressed = false
changeListener?.onStopTrackingTouch(this)
}
MotionEvent.ACTION_MOVE -> {
val progress = (max
- (max * event.y / height).toInt())
setProgress(progress)
onSizeChanged(width, height, 0, 0)
changeListener?.onProgressChanged(this, progress, true)
}
MotionEvent.ACTION_CANCEL -> {
}
}
return true
}
}
这是我如何将 Touch 侦听器添加到 LinearLayout 和 SeekBar:
binding.brightnessContainer.setOnTouchListener(
object : OnTouchListener {
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
// Logic to calculate the swiped position and setting the brighness
return true
}
})
binding.brightnessContainer.seekbar.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(
seekBar: SeekBar?,
progress: Int,
fromUser: Boolean
) {
// logic to calculate current progress
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
// logic to set the current progress
}
}
)
这是向下交换时透明点在 SeekBar 上的显示方式: SeekBar
如果我知道我在这个实现中缺少什么,那将会非常有帮助。提前致谢。
I have added a Vertical SeekBar in my android application which doesn't show the thumb on the SeekBar. This is working all fine but having a small issue - when I scroll/swipe the SeekBar to the bottom, it starts showing the transparent dot the top of SeekBar.
The SeekBar is part of LinearLayout which is also swipe-able (as we want the complete SeekBar component to be swipe-able).
I tried several things like nulling the thumb, setting the SeekBar to non focusable etc but nothing is working.
Here is the Layout of the Brightness control:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/brightness_control_layout_parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/brightness_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mobile_vertical_control_background"
android:orientation="vertical"
android:paddingHorizontal="5dp"
android:paddingVertical="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingVertical="1dp"
android:text="+"
android:textColor="@color/white"
android:textSize="20sp" />
<com.example.view.wigdet.VerticalSeekBar
android:id="@+id/seekbar"
android:layout_width="10dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:progressTint="@color/white"
android:secondaryProgressTint="@color/light_grey"
android:splitTrack="false"
android:thumbTint="@android:color/transparent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingVertical="1dp"
android:text="-"
android:textColor="@color/white"
android:textSize="20sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/brightness_icon" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the Vertical SeekBar View:
class VerticalSeekBar : androidx.appcompat.widget.AppCompatSeekBar {
private var changeListener: OnSeekBarChangeListener? = null
constructor(context: Context?) : super(context!!) {}
constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(
context!!,
attrs,
defStyle
) {
}
constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {}
override fun setOnSeekBarChangeListener(l: OnSeekBarChangeListener) {
this.changeListener = l
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(h, w, oldh, oldw)
}
@Synchronized
override fun setProgress(progress: Int) // it is necessary for calling setProgress on click of a button
{
super.setProgress(progress)
onSizeChanged(width, height, 0, 0)
}
@Synchronized
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec)
setMeasuredDimension(measuredHeight, measuredWidth)
}
override fun onDraw(c: Canvas) {
c.rotate(-90f)
c.translate(-height.toFloat(), 0f)
super.onDraw(c)
}
override fun onTouchEvent(event: MotionEvent): Boolean {
if (!isEnabled) {
return false
}
when (event.action) {
MotionEvent.ACTION_DOWN -> {
isSelected = true
isPressed = true
changeListener?.onStartTrackingTouch(this)
}
MotionEvent.ACTION_UP -> {
isSelected = false
isPressed = false
changeListener?.onStopTrackingTouch(this)
}
MotionEvent.ACTION_MOVE -> {
val progress = (max
- (max * event.y / height).toInt())
setProgress(progress)
onSizeChanged(width, height, 0, 0)
changeListener?.onProgressChanged(this, progress, true)
}
MotionEvent.ACTION_CANCEL -> {
}
}
return true
}
}
Here is how I have added the Touch listener to the LinearLayout and SeekBar:
binding.brightnessContainer.setOnTouchListener(
object : OnTouchListener {
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
// Logic to calculate the swiped position and setting the brighness
return true
}
})
binding.brightnessContainer.seekbar.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(
seekBar: SeekBar?,
progress: Int,
fromUser: Boolean
) {
// logic to calculate current progress
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
// logic to set the current progress
}
}
)
Here is how the transparent dot is showing on the SeekBar when swapped down: SeekBar
It would be really helpful if I get to know what I am missing in this implementation. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试添加透明背景。
例如:
机器人:背景=“#00FFFFFF”
try adding a transparent background.
For example:
android:background="#00FFFFFF"