我对上面接受的答案并不完全满意,所以我自己做了一些额外的研究。
我相信他们使用的技巧是他们在调用的视图层次结构中检索顶部视图并在其中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 的 height 参数将 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>
将适当的 .png 可绘制对象复制sdk/platforms/android-xx/data/res/drawable-xxx/
到您的项目中,然后在您可以添加的代码中:
progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.progress_horizontal_holo_no_background_light));
额外:不确定的进度条
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);
希望我有所帮助:)