我对上面接受的答案并不完全满意,所以我自己做了一些额外的研究。
我相信他们使用的技巧是他们检索了名为DecorView 的视图层次结构中的顶视图,并在其中添加了进度条。这样,进度条会同时显示在操作栏和内容区域上。请注意,SD 的回答是将进度条放入内容区域并从实际内容中“窃取”空间,这可能会导致意外结果。
此实现的示例屏幕截图:
代码
只需将此代码放入某些活动的onCreate 方法中,它应该可以工作:
// create new ProgressBar and style it
final ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
progressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 24));
progressBar.setProgress(65);
// retrieve the top view of our application
final FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
decorView.addView(progressBar);
// Here we try to position the ProgressBar to the correct position by looking
// at the position where content area starts. But during creating time, sizes
// of the components are not set yet, so we have to wait until the components
// has been laid out
// Also note that doing progressBar.setY(136) will not work, because of different
// screen densities and different sizes of actionBar
ViewTreeObserver observer = progressBar.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View contentView = decorView.findViewById(android.R.id.content);
progressBar.setY(contentView.getY() - 10);
ViewTreeObserver observer = progressBar.getViewTreeObserver();
observer.removeOnGlobalLayoutListener(this);
}
});
您可以使用 LayoutParams 的高度参数来设置progressBar 更宽或更窄,但您可能需要调整-10 偏移量。
造型
不幸的是,你可以看到进度条丑陋的灰色背景。要删除它,只需按 id 搜索背景并尝试隐藏它是行不通的。要删除背景,我必须创建与系统版本相同的 drawble 并删除背景项。
TL;DR:创建文件 progress_horizontal_holo_no_background_light.xml 并粘贴此可绘制对象:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/secondaryProgress">
<scale android:scaleWidth="100%"
android:drawable="@drawable/progress_secondary_holo_light" />
</item>
<item android:id="@android:id/progress">
<scale android:scaleWidth="100%"
android:drawable="@drawable/progress_primary_holo_light" />
</item>
</layer-list>
从sdk/platforms/android-xx/data/res/drawable-xxx/复制适当的.png drawables到你的项目,然后在你可以添加的代码中:
progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.progress_horizontal_holo_no_background_light));
额外:不确定的进度条
不定进度条的 Pre-KitKat 版本非常丑陋和滞后。
您可以下载名为ButteryProgressBar 的新平滑进度条。只需在谷歌上搜索它(我不能发布更多链接,因为我是新来的:[),将类添加到您的项目中,您可以简单地用这段代码替换以前的 ProgressBar 并获得清晰的不确定进度条:
final ButteryProgressBar progressBar = new ButteryProgressBar(this);
progressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 24));
您可能还需要简化此代码:
final TypedArray ta = c.obtainStyledAttributes(attrs, R.styleable.ButteryProgressBar);
try {
mBarColor = ta.getColor(R.styleable.ButteryProgressBar_barColor,
c.getResources().getColor(android.R.color.holo_blue_light));
mSolidBarHeight = ta.getDimensionPixelSize(
R.styleable.ButteryProgressBar_barHeight,
Math.round(DEFAULT_BAR_HEIGHT_DP * mDensity));
mSolidBarDetentWidth = ta.getDimensionPixelSize(
R.styleable.ButteryProgressBar_detentWidth,
Math.round(DEFAULT_DETENT_WIDTH_DP * mDensity));
} finally {
ta.recycle();
}
到此代码:
mBarColor = c.getResources().getColor(android.R.color.holo_blue_light);
mSolidBarHeight = Math.round(DEFAULT_BAR_HEIGHT_DP * mDensity);
mSolidBarDetentWidth = Math.round(DEFAULT_DETENT_WIDTH_DP * mDensity);
希望我能帮上忙:)