在您发布 stackoverflow 答案的链接之前,请注意我可能已经尝试过。
我使用 android studio 和 kotlin。
custom_lvlmeter_channel.xml
只是 FrameLayout 内的 1 个垂直搜索栏,必须填充其可用的整个空间。换句话说,我想要一个没有拇指的非常厚的搜索栏。到目前为止我已经尝试了不同的方法但没有成功。我的问题是,它没有一个非常宽的搜索栏,而是在超过某个点后停止变宽,并用......空白空间(背景颜色为蓝色)填充其余空间。 。
自定义*_*lvlmeter_channel.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/flSeekbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#59A2DC"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.example.levelmeter.widgetlogic.LevelMeterChannel
android:id="@+id/sbFader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:progress="10"
android:progressDrawable="@drawable/draw_custom_lvlmeter_channel"
android:rotation="270"
android:thumb="@null"
/>
</FrameLayout>
(可绘制)draw_custom_lvlmeter_channel.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#ffe9e9e9"
android:centerColor="#ffc6c6c6"
android:centerY="0.75"
android:endColor="#ffe9e9e9"
android:angle="270"/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#ffe9e9e9"
android:endColor="#ff2165ca"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
LvlMeterChannel.kt
//这是一个未更改的搜索栏。唯一被覆盖的功能是宽度现在也缩放到父级的高度。这样做是因为搜索栏被旋转,因此它的宽度现在是垂直的。
package com.example.levelmeter.widgetlogic
import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatSeekBar
class LevelMeterChannel(context: Context, attrs: AttributeSet) : AppCompatSeekBar(context, attrs) {
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(heightMeasureSpec, heightMeasureSpec)
}
}
/////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////// //////////////////////////
以下是我复制并尝试过的 2 个解决方案:
https://www.youtube.com/watch?v=O48ahJPTvJc&t=1s
所有改变的是我的搜索栏的颜色,问题仍然存在,而这些教程似乎对其他人有用。
解决方案
尝试使用材料设计滑块。他们为你的滑块(他们的搜索栏的名称)提供了更简单、直接和灵活的定制方式,并且会让你省心。可以通过在 app:trackHeight 下设置您选择的任何值来更改滑块的厚度,而不是通过可绘制对象等操作它。