【问题标题】:Changing the size of the seekbar programmatically but cant get the thumb to be larger than the bar以编程方式更改搜索栏的大小,但不能让拇指大于栏
【发布时间】:2012-10-20 23:07:26
【问题描述】:

我必须制作一个搜索栏,我可以在其中以编程方式更改几乎所有内容。现在我在更改拇指和条形大小时遇到​​了问题。如果条形图较大或与拇指大小相同,则一切正常,但如果拇指大于条形图,我无法让它以正确大小的条形图显示整个拇指。

这是我创建拇指的代码:

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

这是我创建栏的代码:

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

  }

这是我将所有内容放在一起的代码,我在其中设置了搜索栏的高度:

  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 代码:

  <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(seekbar, params)。
添加视图时,如果我使用我想要的高度,拇指会被切割。如果我使用拇指的高度,则条形图的高度与拇指的高度相同。 Android 将栏的大小调整为我用于添加视图的大小。
我尝试使用 shape.setSize(width, height) 设置作为条形的 GradientDrawable 的大小,我还尝试了shape.setBounds(0, 0, width, height),我尝试创建另一个具有正确大小的图像并添加到 LayerDrawable mylayer。但没有任何效果。

当通过 XML 创建它时,可以使用 paddingToppaddingBottom 将条形设置为正确的大小。但我不知道如何以编程方式对 GradientDrawable 执行此操作。

【问题讨论】:

    标签: android scrollbar seekbar


    【解决方案1】:

    好吧,太糟糕了,没有回应,但我自己能够回答这个问题。答案是使用 InsetDrawable。您必须将栏的其中一个可绘制对象放入 InsetDrawable。

    代码如下:

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

    我希望这可以帮助其他有同样问题的人。

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多