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.
Dobra robota n Dzięki –