gpt4 book ai didi

android - 操作栏下的进度条

转载 作者:IT老高 更新时间:2023-10-28 13:10:28 25 4
gpt4 key购买 nike

问题摘要:我怎样才能使 ProgressBar 集成在 ActionBar 中,就像在 Chrome 应用程序上一样?

详情:看看这个来自 Chrome 的截图:

Chrome for Android screenshot

我想创建一个像这样的操作栏。就在操作栏下方,有一个根据页面加载填充的进度条。我从许多应用程序(例如 Feedly)中看到了这个示例,但我无法创建自己的实现。我尝试使用 Android 自己的 API 来创建它:

@Override
protected void onCreate(Bundle savedInstanceState) {
//Request Permission to display the Progress Bar...
this.requestWindowFeature(Window.FEATURE_PROGRESS);
this.setWindowContentView(R.layout.activity_main)
super.onCreate(savedInstanceState);

this.setProgressBarIndeterminate(true);
}

但这段代码只会导致 ProgressBar 操作栏上显示,如下所示:

My App Screenshot

那么,我怎样才能让我的 ProgressBar 出现在操作栏下方,就像在 Chrome 应用程序上一样?

最佳答案

我对上面接受的答案并不完全满意,所以我自己做了一些额外的研究。

我相信他们使用的技巧是,他们检索了名为 DecorView 的 View 层次结构中的顶部 View ,并在其中添加了进度条。这样,进度条会同时显示在操作栏和内容区域上。请注意,SD 的回答是将进度条放入内容区域并从实际内容中“窃取”空间,这可能会导致意外结果。

此实现的示例屏幕截图:

ProgressBar

ProgressBarIndeterminate

代码

只需将此代码放入某些 Activity 的 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_horizo​​ntal_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 可绘制对象复制到您的项目中,然后在您可以添加的代码中:

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

希望我能帮上忙:)

关于android - 操作栏下的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13934010/

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