我找到了另一种实现自定义搜索栏的方法。这是我目前正在使用的代码
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
public class FlatSeekBar extends android.widget.SeekBar
{
private int size = 20;
public FlatSeekBar(Context context)
{
super(context);
init(null, true);
}
public FlatSeekBar(Context context, AttributeSet attrs)
{
super(context, attrs);
init(attrs, true);
}
public FlatSeekBar(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init(attrs, true);
}
private void init(AttributeSet attrs, boolean applyAttributeTheme)
{
// setting thumb
PaintDrawable thumb = new PaintDrawable(Color.parseColor("#2C3E50"));
thumb.setCornerRadius(size * 9 / 8);
thumb.setIntrinsicWidth(size * 9 / 4);
thumb.setIntrinsicHeight(size * 9 / 4);
setThumb(thumb);
// progress
PaintDrawable progress = new PaintDrawable(Color.parseColor("#34495E"));
progress.setCornerRadius(size);
progress.setIntrinsicHeight(size);
progress.setIntrinsicWidth(size);
progress.setDither(true);
ClipDrawable progressClip = new ClipDrawable(progress, Gravity.LEFT, ClipDrawable.HORIZONTAL);
// secondary progress
PaintDrawable secondary = new PaintDrawable(Color.parseColor("#EBEDEF"));
secondary.setCornerRadius(size);
secondary.setIntrinsicHeight(size);
ClipDrawable secondaryProgressClip = new ClipDrawable(secondary, Gravity.LEFT, ClipDrawable.HORIZONTAL);
// background
PaintDrawable background = new PaintDrawable(Color.parseColor("#EBEDEF"));
background.setCornerRadius(size);
background.setIntrinsicHeight(size);
// applying drawable
LayerDrawable ld = (LayerDrawable) getProgressDrawable();
ld.setDrawableByLayerId(android.R.id.background, background);
ld.setDrawableByLayerId(android.R.id.progress, progressClip);
ld.setDrawableByLayerId(android.R.id.secondaryProgress, secondaryProgressClip);
}
}
只需编辑这四种颜色即可获得您自己的颜色:
#2C3E50
#34495E
#EBEDEF
#EBEDEF
我希望它有帮助;)