【问题标题】:Android seekbar widget: no vertical orientation?Android seekbar 小部件:没有垂直方向?
【发布时间】:2010-03-24 07:54:46
【问题描述】:

我不敢相信没有办法改变 SeekBar 小部件的方向。我一直在仔细研究这个小部件的属性,但找不到任何可以让我将其方向更改为垂直的东西。

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

【问题讨论】:

    标签: java android android-widget seekbar


    【解决方案1】:

    有两种方法可以实现目标

    1- 使用 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>
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 2017-04-29
      • 2017-02-22
      • 2012-04-20
      • 2016-06-10
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多