実行時にプライマリとセカンダリの進行状況で SeekBar を作成する必要があります。LayerDrawableをどのように扱うべきかについてtileify 関数を見て、適切な ID についてはprogress_horizo ntal_holo_dark.xml を見て、私は次のことを思いつきました:
// 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
問題:
- setProgressDrawable(layerDrawable) が期待どおりに機能しません。私はこの関数を XML コードで使用していますが、実行時に使用すると、下の図に示すように resultult が得られます。
何か案は?