2010-10-06 13 views
2

Je veux afficher un TextView juste ci-dessus pouce du SeekBar qui affichera la progression actuelle de la barre de recherche. Lorsque nous déplaçons le pouce vers l'avant et vers l'arrière, le TextView se déplace également avec le pouce (le TextView doit également être au-dessus du pouce).comment personnaliser le pouce de la barre de recherche

S'il vous plaît fournir toute idée, extrait de code sont toujours les bienvenus.

Répondre

3

Mise en page de votre activité

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
     <!-- Main context --> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <SeekBar android:id="@+id/skbSample" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:max="35"> 
     </SeekBar> 
    </LinearLayout> 
     <!-- For display value --> 
    <AbsoluteLayout android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TextView android:text="0" 
      android:id="@+id/txvSeekBarValue" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFFFFFFF" 
      android:background="#FF777777" 
      android:visibility = "invisible"> 
     </TextView> 
    </AbsoluteLayout> 
</FrameLayout> 

vos contrôles sur Initialize créer:

mTxvSeekBarValue = (TextView) this.findViewById(R.id.txvSeekBarValue); 
mSkbSample = (SeekBar) this.findViewById(R.id.skbSample); 
mSkbSample.setOnTouchListener(new OnTouchListener() {  
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
     if(event.getAction() == MotionEvent.ACTION_DOWN) 
     { 
      ShowSeekValue((int)event.getX(), mTxvSeekBarValue.getTop()); 
      mTxvSeekBarValue.setVisibility(View.VISIBLE); 
     } 
     else if(event.getAction() == MotionEvent.ACTION_MOVE) 
     { 
      ShowSeekValue((int)event.getX(), mTxvSeekBarValue.getTop()); 
     } 
     else if(event.getAction() == MotionEvent.ACTION_UP) 
     { 
      mTxvSeekBarValue.setVisibility(View.INVISIBLE); 
     } 
     return false; 
    } 
}); 

Fonction déplacera votre valeur:

private void ShowSeekValue(int x, int y) 
{ 
    if(x > 0 && x < mSkbSample.getWidth()) 
    { 
     AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, x, y); 
     mTxvSeekBarValue.setLayoutParams(lp); 
    } 
} 
1

trouvé une meilleure façon. Dérivé de l'échantillon:

mTxvSeekBarValue = (TextView) this.findViewById(R.id.txvSeekBarValue); 
mSkbSample = (SeekBar) this.findViewById(R.id.skbSample); 
mSkbSample.setOnTouchListener(new OnTouchListener() {  
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
     if(event.getAction() == MotionEvent.ACTION_DOWN) 
     { 
      int progress = mSkbSample.getProgress(); 
    mTxvSeekBarValue.setText(String.valueOf(progress).toCharArray(), 0, String.valueOf(progress).length()); 
     } 
     else if(event.getAction() == MotionEvent.ACTION_MOVE) 
     { 
      int progress = mSkbSample.getProgress(); 
    mTxvSeekBarValue.setText(String.valueOf(progress).toCharArray(), 0, String.valueOf(progress).length()); 
     } 
     return false; 
    } 
}); 

Il vous n'avez pas besoin méthode supplémentaire et l'événement ACTION_UP n'a pas d'effet important sur le résultat.

10

Le déplacement du texte peut encore plus facilement fait en réglant le padding pour le TextView comme:

int xPos = ((SeekbarInstance.getRight() - SeekbarInstance.getLeft()) * SeekbarInstance.getProgress())/SeekbarInstance.getMax(); 
textViewInstance.setPadding(xPos, 0, 0, 0); 
+0

homme impressionnant. Exactement ce que je voulais. – Rajkiran