2016-04-18 9 views
1

私のニーズに合わせてシークバーをカスタマイズしたい:私は後述のように、顧客のシークバーを定義したいのですが、どのように?

親指は、4つの固定場所にのみ配置できます。私は120にシークバーの最大プロパティを設定した場合、たとえば、4つの固定位置は0、40、80 120 また、私は人々に現在選択した場所の手段を伝えるための指標を持つようにしたいです。次のように私はそれを実行します。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:id="@+id/ll_id" 
     android:paddingLeft="10dp"> 
     <TextView 
      android:id="@+id/tv1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="small"/> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:id="@+id/tv2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="middle"/> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:id="@+id/tv3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="big"/> 
     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
     <TextView 
      android:id="@+id/tv4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="extrabig"/> 
    </LinearLayout> 
    <SeekBar android:id="@+id/sk" 
     android:layout_below="@+id/ll_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:max="120" 
     android:paddingLeft="0dp" 
     android:paddingRight="0dp" 
     android:layout_marginRight="0dp"/> 
</RelativeLayout> 


public class CustomSeekBar extends FrameLayout { 

    private SeekBar sk; 
    private OnWhichSelected selected; 

    public CustomSeekBar(Context context) { 
     this(context, null); 
    } 

    public CustomSeekBar(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     LayoutInflater.from(context).inflate(R.layout.customseekbar_layout, this, true); 
     sk = (SeekBar) this.findViewById(R.id.sk); 
     sk.setProgress(0); 
     callbackhelp(Which.SMALL); 
     sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ 
      @Override 
      public void onProgressChanged(SeekBar seekBar, int progress, 
        boolean fromUser) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void onStartTrackingTouch(SeekBar seekBar) { 
       // TODO Auto-generated method stub 
      } 

      @Override 
      public void onStopTrackingTouch(SeekBar seekBar) { 
       // TODO Auto-generated method stub 
       int seekProgress = seekBar.getProgress(); 
       if(seekProgress<seekBar.getMax()/6){ 
        callbackhelp(Which.SMALL); 
        seekBar.setProgress(0); 
       }else if(seekProgress>=seekBar.getMax()/6 && seekProgress<3*(seekBar.getMax()/6)){ 
        callbackhelp(Which.MIDDLE); 
        seekBar.setProgress(seekBar.getMax()/3); 
       }else if(seekProgress>=3*(seekBar.getMax()/6) && seekProgress<5*(seekBar.getMax()/6)){ 
        callbackhelp(Which.LARGE); 
        seekBar.setProgress(4*(seekBar.getMax()/6)); 
       }else if(seekProgress>=5*(seekBar.getMax()/6)){ 
        callbackhelp(Which.EX_LARGE); 
        seekBar.setProgress(seekBar.getMax()); 
       } 
      } 
     }); 
    } 


    private void callbackhelp(Which which){ 
     if(selected!=null){ 
      selected.onSeleted(which); 
     } 
    } 

    public enum Which{ 
     SMALL, 
     MIDDLE, 
     LARGE, 
     EX_LARGE 
    } 

    public interface OnWhichSelected{ 
     void onSeleted(Which which); 
    } 
} 

今、結果はこのようなものです:どのように
結果
enter image description here

あなたは、親指が「中」のインジケータの下に中央を合わせていない見ることができます親指が異なるデバイス上のすべての指標の下に中央揃えすることができように、この問題を解決?おかげ

+0

誰も私を助けて?おかげ –

答えて

0

は、それはあなたを助けるかもしれ次のコードを使用して線形のレイアウトを交換してください。

<LinearLayout 
     android:id="@+id/ll_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingLeft="10dp"> 

     <TextView 
      android:id="@+id/tv1" 
      android:gravity="center" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="small" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <TextView 
      android:id="@+id/tv2" 
      android:gravity="center" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="middle" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <TextView 
      android:id="@+id/tv3" 
      android:layout_width="0dp" 
      android:gravity="center" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="big" /> 

     <View 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <TextView 
      android:id="@+id/tv4" 
      android:gravity="center" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="extrabig" /> 
    </LinearLayout> 
+0

はあなたに感謝し、それが動作しません。 –

関連する問題