2010-03-24 102 views
3

我不能相信這是沒有辦法改變的搜索欄控件的方向。我一直在細讀這個小部件的屬性,我找不到任何可以讓我將其方向改變爲垂直的東西。Android seekbar widget:沒有垂直方向?

那麼,我錯過了一些明顯的東西?我是否必須編寫我自己的seekbar實現來使其拇指向上/向下滑動而不是左/右?

回答

1

有兩種方式才達到的目標

1- Uing XML 對於API大於11 API

<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>