Android搜索栏小部件:没有垂直方向?

时间:2010-03-24 07:54:46

标签: java android android-widget seekbar

我无法相信没有办法改变SeekBar小部件的方向。我一直在仔细阅读这个小部件的属性,我找不到任何允许我将其方向改为垂直方向的东西。

那么,我错过了一些明显的东西吗?我是否必须编写自己的搜索栏实现,让拇指向上/向下滑动而不是向左/向右滑动?

1 个答案:

答案 0 :(得分:1)

实现目标有两种方法

1- Uing xml 对于api大于api 11

<com.yourPackge.VerticalSeekBar
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="bottom"
             android:id="@+id/verticalseekbar"            
            android:max="10"
            android:rotation="270" 
           />

2-创建自定义垂直搜索栏

public class VerticalSeekBar extends SeekBar {
    public VerticalSeekBar(Context context) {
        super(context);    }

    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);}

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);}

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);}
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());}

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);
        super.onDraw(c);}

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;}

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                setProgress(getMax() - (int) (getMax() * event.getY() /getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;
            case MotionEvent.ACTION_CANCEL:
               break;
        }
        return true;
    }

    public BitmapDrawable writeOnDrawable(int drawableId, String text){

        Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLACK);
        paint.setTextSize(20);
        Canvas canvas = new Canvas(bm);
        canvas.drawText(text, 0, bm.getHeight()/2, paint);

        return new BitmapDrawable(bm);
    }}

如果要创建自定义进度条和拇指
在您要添加搜索栏的活动中

 <SeekBar
    android:progressDrawable="@drawable/progress"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:max="10"
    android:id="@+id/seekBar4"
    android:thumb="@drawable/tumb_shape"/>

tump_shap.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <gradient
        android:angle="270"
        android:endColor="#00ACE7"
        android:startColor="#0086B3" />

    <size
        android:height="20dp"
        android:width="20dp" />

</shape>

progress.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"
        android:drawable="@drawable/background_fill"/>
    <item android:id="@android:id/progress">
        <clip android:drawable="@drawable/progress_fill" />
    </item>

</layer-list>