2012-10-31 10 views
7

Muszę zrobić pasek wyszukiwania, gdzie mogę zmienić prawie wszystko programowo. A teraz mam problem z kciukiem i rozmiarem pręta. Wszystko działa świetnie, jeśli pasek jest większy lub taki sam jak kciuk, ale jeśli kciuk jest większy niż pasek, nie mogę go wyświetlić, aby pokazać cały kciuk o prawidłowym rozmiarze paska.Zmieniając rozmiar paska wyszukiwania programowo, ale nie mogę uzyskać kciuka, aby był większy niż pasek

Oto mój kod, aby utworzyć zasada:

public void setSeekBarThumb(int width, int height, int rColor, int gColor, int bColor){ 

     ShapeDrawable thumb = new ShapeDrawable(new OvalShape()); 
     thumb.setIntrinsicHeight(height); 
     thumb.setIntrinsicWidth(width); 
     thumb.setBounds(new Rect(0, 0, width, height)); 


     StateListDrawable states = new StateListDrawable(); 
     states.addState(new int[] {android.R.attr.state_pressed},thumb );//add to the state list with the state pressed 

     thumb = new ShapeDrawable(new OvalShape()); 
     thumb.setIntrinsicHeight(height); 
     thumb.setIntrinsicWidth(width); 
     thumb.setBounds(new Rect(0, 0, width, height)); 

     states.addState(new int[] { },thumb);//add to the state list with no state 

     seekbar.setThumb(states); 
     seekbar.setThumbOffset(0); 
    } 

heres mojego kodu do tworzenia paska:

public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){ 
     GradientDrawable shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)}); 
     shape.setCornerRadius(50); 
     shape.setSize(width, height); 
     shape.setBounds(0, 0, width, height); 
     ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,ClipDrawable.HORIZONTAL); 

     shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)}); 
     shape.setCornerRadius(50);//change the corners of the rectangle 
     shape.setSize(width, height); 
     shape.setBounds(0, 0, width, height); 

     LayerDrawable mylayer= new LayerDrawable(new Drawable[]{shape, clip,}); 

     seekbar.setProgressDrawable(mylayer); 
     seekbar.setProgress(50); 

    } 

Tu jest mój kod, aby umieścić wszystko razem, gdzie mogę ustawić wysokość szukać bar:

public MySeekBar(Activity activity, AbsoluteLayout ParentLayout, SeekBarParameters parameters) 
    {   
     super(activity.getApplicationContext()); 



     seekbar =(SeekBar)activity.getLayoutInflater().inflate(R.layout.horizontal_seek_bar, ParentLayout,false); 
     seekbar.setMax(100);  
     setSeekBarThumb(parameters.widthThumb, parameters.heightThumb,0,100,0); 
     setSeekBarProgress(parameters.width, parameters.height,255,69,0); 

     int drawHeight; 
     if(parameters.height>parameters.heightThumb) drawHeight=parameters.height; 
     else drawHeight=parameters.heightThumb; 

     AbsoluteLayout.LayoutParams params = new AsoluteLayout.LayoutParams(parameters.width,drawHeight,parameters.xPosition, parameters.yPosition); 
     ParentLayout.addView(seekbar, params);      

    } 

Kod XML gdzie nadmuchać barze od:

<SeekBar xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:max="100" 
     android:progress="0" 
     android:secondaryProgress="0" 
     android:maxHeight="10000dip" 
     android:id="@+id/slider" 
    /> 

Aby zmienić rozmiar paska wyszukiwania, używam ParentLayout.addView (seekbar, params).
Podczas dodawania widoku, jeśli używam wysokości, która jest potrzebna na pasku, kciuk zostanie obcięty. Jeśli używam wysokości kciuka, pasek dociera do tej samej wysokości kciuka. Android zmienia rozmiar paska na rozmiar, którego używam do dodania widoku.
Próbowałem ustawić rozmiar GradientDrawable, który jest pasek z s hape.setSize(width, height), Próbowałem również shape.setBounds(0, 0, width, height), i próbowałem stworzyć kolejny obraz o odpowiednim rozmiarze i dodanie do Warstwy LayerDrawable. Ale nic nie działało.

Podczas tworzenia tego poprzez XML możliwe jest użycie paddingTop i paddingBottom, aby dopasować pasek do właściwego rozmiaru. Ale nie wiem, jak to zrobić programowo w GradientDrawable.

Odpowiedz

15

Cóż, szkoda, że ​​nie było odpowiedzi, ale sam byłem w stanie odpowiedzieć na to pytanie. Odpowiedź brzmi: użyj InsetDrawable. Musisz umieścić jeden z rysunków paska w pliku InsetDrawable.

Ów kod:

public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){ 
     GradientDrawable shape2 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)}); 
     shape2.setCornerRadius(50);      
     ClipDrawable clip = new ClipDrawable(shape2, Gravity.LEFT,ClipDrawable.HORIZONTAL); 

     GradientDrawable shape1 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)}); 
     shape1.setCornerRadius(50);//change the corners of the rectangle  
     InsetDrawable d1= new InsetDrawable(shape1,5,5,5,5);//the padding u want to use   


     LayerDrawable mylayer = new LayerDrawable(new Drawable[]{d1,clip}); 


     seekbar.setProgressDrawable(mylayer); 

     seekbar.setProgress(50); 

    } 

Mam nadzieję, że może to pomóc komuś z tym samym problemem.

+0

Dobra robota n Dzięki –

Powiązane problemy