我必须在运行时创建具有主要和次要进度的SeekBar。查看tileify function我应该如何处理LayerDrawable,progress_horizontal_holo_dark.xml查看正确的ID,我想出了以下内容:
// parent view
LinearLayout parent = (LinearLayout) findViewById(R.id.parent);
// create widget
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
SeekBar seekBar = new SeekBar(this);
seekBar.setLayoutParams(params);
// set bg resources
Drawable background = getResources().getDrawable(R.drawable.scrubber_track_holo_light);
ScaleDrawable primary = new ScaleDrawable(getResources().getDrawable(R.drawable.scrubber_primary_holo), 0x10|0x03, 100.0f, 100);
ScaleDrawable secondary = new ScaleDrawable(getResources().getDrawable(R.drawable.scrubber_secondary_holo), 0x100x03, 100.0f, 85);
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { background, secondary, primary});
layerDrawable.setId(0, android.R.id.background);
layerDrawable.setId(1, android.R.id.secondaryProgress);
layerDrawable.setId(2, android.R.id.progress);
seekBar.setBackgroundDrawable(layerDrawable);
seekBar.setThumb(getResources().getDrawable(R.drawable.seekbar_btn));
// set values
seekBar.setMax(50);
seekBar.setSecondaryProgress(25);
seekBar.setProgress(15);
parent.addView(seekBar);
这就是SeekBar的样子:
这是我的资源档案:
- scrubber_primary_holo.9.png
- scrubber_secondary_holo.9.png
问题:
有什么想法吗?
答案 0 :(得分:1)
如果其他人遇到同样的问题,我找到了解决方案。有两个问题。第一个是虽然在XML中使用的ScaleBitmap不能从代码中工作。我不得不使用ClipDrawable。第二个问题是显示主要和次要进展 - 它们是在第一次进展变化后显示的。通过首先将seekBar添加到父视图然后设置主要和次要进度来解决此问题。您可以在下面看到源代码。干杯!
// CREATE WIDGET
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
SeekBar seekBar = new SeekBar(this, null, android.R.attr.seekBarStyle);
seekBar.setLayoutParams(params);
parent.addView(seekBar);
seekBar.setMax(50);
seekBar.setProgress(15);
seekBar.setSecondaryProgress(25);
// CREATE PROGRESS BAR
LayerDrawable layerDrawable = (LayerDrawable) seekBar.getProgressDrawable();
seekBar.setThumb(getResources().getDrawable(R.drawable.seekbar_btn));
Drawable progress = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.progress);
Bitmap progressBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_secondary_holo);
progress = new ClipDrawable(new BitmapDrawable(getResources(), progressBmp), Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);
layerDrawable.setDrawableByLayerId(android.R.id.progress, progress);
Drawable secondary = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.secondaryProgress);
Bitmap secondaryBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_primary_holo);
secondary = new ClipDrawable(new BitmapDrawable(getResources(), secondaryBmp), Gravity.AXIS_PULL_BEFORE, ClipDrawable.HORIZONTAL);
layerDrawable.setDrawableByLayerId(android.R.id.secondaryProgress, secondary);
Drawable background = (Drawable) layerDrawable.findDrawableByLayerId(android.R.id.background);
Bitmap backgroundBmp = BitmapFactory.decodeResource(getResources(), R.drawable.scrubber_track_holo_light);
background = new BitmapDrawable(getResources(), backgroundBmp);
layerDrawable.setDrawableByLayerId(android.R.id.background, background);