gpt4 book ai didi

android - 为什么动态创建的SeekBar不带我的颜色?

转载 作者:行者123 更新时间:2023-11-30 02:48:21 25 4
gpt4 key购买 nike

我必须在运行时创建具有主要和次要进度的 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 应该是这样的:

ideal

这是我的资源文件:

primary - scrubber_primary_holo.9.png
secondary - scrubber_secondary_holo.9.png

问题:

  • setProgressDrawable(layerDrawable) 没有按预期工作。我在 XML 代码中使用这个函数,但如果我在运行时使用它,我会得到如下图所示的结果。

enter image description here

有什么想法吗?

最佳答案

如果其他人遇到同样的问题,我找到了解决方案。有两个问题。第一个是 ScaleBitmap 虽然在 XML 中使用但不能从代码中使用。我不得不使用 ClipDrawable。第二个问题是显示主要和次要进度 - 它们在第一次进度更改后显示。这是通过先将seekBar添加到parent view然后设置primary和secondary progress来解决的。您可以在下面查看源代码。干杯!

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

关于android - 为什么动态创建的SeekBar不带我的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24630566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com