有没有办法在SeekBar Android小部件中删除背景条图像?
我只想显示一个没有进度条的滑块。
有什么帮助吗?
有一种更简单的方法,不需要创建新的drawable。只需在xml定义中设置以下内容:
android:progressDrawable="@android:color/transparent"
或者如果你想在代码中完成它:
mySlider.setProgressDrawable(new ColorDrawable(android.R.color.transparent));
我解决了自己的问题。
要去除背景,您需要创建一个空白的可绘制对象,并将其设置为progressDrawable:
satBar.setProgressDrawable(invisibleBackground);
Have you tried setBackgroundDrawable(null)
?
getBackground()
,将结果保存在某个地方,然后通过setBackgroundDrawable()
恢复它。尽管名称不完全相同,但getBackground()
返回一个Drawable
。 - CommonsWare一种实现这个目标的方法是将进度条的颜色改为背景颜色。您需要使用自定义布局。
<SeekBar
android:layout_width="350px"
android:layout_height="25px"
android:layout_margin="30px"
android:layout_x="60px"
android:layout_y="185px"
android:id="@+id/seekbar"
android:progressDrawable="@drawable/myprogress"
/>
<?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>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item
android:id="@android:id/progress"
>
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="#9191FE"
android:centerColor="#9191FE"
android:centerY="0.75"
android:endColor="#9191FE"
android:angle="270" />
</shape>
</clip>
</item>
seekbar_progress.xml
的 XML 文件,并将其放置在 drawable
文件夹中。其中,条纹是一张图片,请将其放置在 drawable
文件夹中。
seekbar_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<clip>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/stripes"
android:tileMode="repeat"
android:antialias="true"
android:dither="true"
android:filter="false"
android:gravity="left"
/>
</clip>
</item>
</layer-list>
现在,在您的main xml
文件中,包含seekbar代码,取一张图片并存储在drawable
文件夹中。这里的seekbar是一张图片。
<SeekBar
android:id="@+id/seekbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/seekbar"
android:progressDrawable="@drawable/seekbar_progress"/>