如何在android中将搜索栏的拇指对中?

时间:2015-10-09 19:34:18

标签: android nativescript

Picture of seekbar

我设法在android中以编程方式创建了搜索栏,但我坚持将矩形拇指对中并正确调整它的高度。我该如何将拇指放在中心?

编辑:附加代码 注意:代码是用javascript编写的,但它等同于java中可以完成的操作(我正在使用可以完成的环境)

function setSeekBarProgress(){

    var shape=new android.graphics.drawable.GradientDrawable();
    shape.setOrientation(android.graphics.drawable.GradientDrawable.Orientation.BOTTOM_TOP);
    shape.setColor(android.graphics.Color.BLUE);
    shape.setCornerRadius(50);
    shape.setSize(600,200);
    shape.setBounds(0,0,600,200);

    var clip=new android.graphics.drawable.ClipDrawable(shape,android.view.Gravity.LEFT,android.graphics.drawable.ClipDrawable.HORIZONTAL);



    shape=new android.graphics.drawable.GradientDrawable();
    shape.setOrientation(android.graphics.drawable.GradientDrawable.Orientation.BOTTOM_TOP);
    shape.setColor(android.graphics.Color.BLACK);

    shape.setCornerRadius(50);
    shape.setSize(1000,200);
    shape.setBounds(0,0,1000,200);
    var insetDrawable=new android.graphics.drawable.InsetDrawable(shape,5,5,5,5);

    var a=[];
    var drawableArray= java.lang.reflect.Array.newInstance(android.graphics.drawable.Drawable.class,2);
    drawableArray[0]=insetDrawable;
    drawableArray[1]=clip;

    var myLayer = new android.graphics.drawable.LayerDrawable(drawableArray);

    nativeSeekBar.setProgressDrawable(myLayer);

}

创建搜索栏的代码:

           nativeSeekBar=new android.widget.SeekBar(args.context,null, android.R.attr.progressBarStyleHorizontal);
            setSeekBarProgress();
            var RoundRecFirstArg= java.lang.reflect.Array.newInstance(java.lang.Float.class.getField("TYPE").get(null),8);
            RoundRecFirstArg[0]=2;
            RoundRecFirstArg[1]=2;
            RoundRecFirstArg[2]=2;
            RoundRecFirstArg[3]=2;
            RoundRecFirstArg[4]=2;
            RoundRecFirstArg[5]=2;
            RoundRecFirstArg[6]=2;
            RoundRecFirstArg[7]=2;
            var thumb=new android.graphics.drawable.ShapeDrawable(new android.graphics.drawable.shapes.RoundRectShape(RoundRecFirstArg,null,null));
                           thumb.getPaint().setColor(android.graphics.Color.rgb(133,133,133));
            thumb.setIntrinsicHeight(200);
            thumb.setIntrinsicWidth(80);
            nativeSeekBar.setThumb(thumb);
            nativeSeekBar.setProgress(20);

1 个答案:

答案 0 :(得分:0)

您需要将thumb的高度设置为seekBar的高度。为了使您的thumb比您的seekBar更“薄”,我创建了一个位图,其高度为seekBar,宽度为drawable。然后,使用thumb

设置Drawable.setBounds()在中间的位置