改变搜索栏的大小编程,但不能让拇指比杆大 [英] Changing the size of the seekbar programmatically but cant get the thumb to be larger than the bar

查看:262
本文介绍了改变搜索栏的大小编程,但不能让拇指比杆大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做出一个搜索栏在那里我可以以编程方式更改几乎所有的东西。现在我有一个问题,改变拇指与酒吧大小。一切都很正常,如果酒吧是大于或相同大小的拇指,但如果拇指比杆大的,我不能得到它显示了吧正确的尺寸,整个大拇指。

I have to make a seekbar where i can change almost everything programmatically. And now I'm having a problem to change the thumb and the bar size. Everything works great if the bar is larger or the same size as the thumb, but if the thumb is larger than the bar, I can't get it to show the entire thumb with the correct size of the bar.

下面是我的code创建了大拇指:

Here's my code to create the thumb:

   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);
  }

我的继承人code创建的吧:

heres my code to create the bar:

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

  }

下面是我的code将一切融合在一起,在这里我设置了搜索栏的高度:

Here is my code to put everything together, where I set the height of the seek 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);                       

  }

中的XML code,我吹了吧,从:

The XML code where I inflate the bar from:

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

要改变搜索栏我用ParentLayout.addView(搜索栏,则params)的大小。
当添加的看法,如果我使用,我想了吧高度,拇指被板缺。如果我用拇指的高度,酒吧里的拇指相同的高度。 Android的调整大小酒吧里我用添加视图​​的大小。
我trye​​d设置GradientDrawable的大小与S酒吧 hape.setSize(宽,高),我也试过 shape.setBounds(0 ,0,宽度,高度),我试图创建另一个图像的大小合适,并增加了LayerDrawable mylayer。但毫无效果。

To change the size of the seek bar I use ParentLayout.addView(seekbar, params).
When adding the view, if I use the height that I want to the bar, the thumb gets cutted. If I use the height of the thumb, the bar gets to the same height of the thumb. Android resizes the bar to the size I use to add the view.
I tryed setting the size of the GradientDrawable that is the bar with shape.setSize(width, height), I also tried shape.setBounds(0, 0, width, height), and I tried creating another image with the right size and adding to the LayerDrawable mylayer. But nothing worked.

在通过XML创建这个有可能使用 paddingTop paddingBottom会拿到扎到合适的尺寸。但我不知道如何做到这一点的GradientDrawable编程。

When creating this through XML it's possible to use paddingTop and paddingBottom to get the bar to the right size. But I don't know how to do this to the GradientDrawable programmatically.

推荐答案

好了,太糟糕了没有反应,但我能回答这个问题我自己。答案是使用InsetDrawable。你必须把酒吧变成InsetDrawable的可绘制的之一。

well, too bad there were no responses but i was able to answer the question myself. The answer is to use InsetDrawable. You have to put the one of the drawables of the bar into a InsetDrawable.

多数民众赞成在code:

Thats the code:

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

  }

我希望这能帮助别人同样的问题。

I hope this can help someone else with the same problem.

这篇关于改变搜索栏的大小编程,但不能让拇指比杆大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆