android - 以编程方式更改搜索栏的大小但无法使拇指大于栏

标签 android scrollbar seekbar

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

这是我创建缩略图的代码:

   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)。
添加 View 时,如果我使用我想要的高度,拇指就会被切断。如果我使用拇指的高度,则杆会达到与拇指相同的高度。 Android 将栏的大小调整为我用来添加 View 的大小。
我尝试使用 shape.setSize(width, height) 设置 GradientDrawable 的大小,我还尝试了 shape.setBounds(0, 0, width, height),我尝试创建另一个大小合适的图像并添加到 LayerDrawable mylayer。但没有任何效果。

当通过 XML 创建时,可以使用 paddingToppaddingBottom 使栏达到正确的大小。但我不知道如何以编程方式对 GradientDrawable 执行此操作。

最佳答案

好吧,太糟糕了,没有任何回应,但我能够自己回答这个问题。答案是使用 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);

  }

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

关于android - 以编程方式更改搜索栏的大小但无法使拇指大于栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13158831/

相关文章:

android - 为什么我的 Dialog Seekbar 崩溃了?空指针异常

Android Phonegap 插件和谷歌分析

java - 替换android中文件的第一行

css - 文本在悬停滚动条上跳跃

jquery - Chrome : weird scroll bar only during certain transition effects

java - 在 >=23 中膨胀类 Seekbar 时出错

java - 设置颜色搜索栏

android - 我如何在 Android SDK 中获取文件?

android - android 5.0 switch 报错

html - 如何在Firefox 和IE 中显示滚动条?