我Button
上有一个Activity
。如果用户按下此Button
,请启动一些网络操作。我想要晦涩的内容并在中心显示进度条。
我目前的实施方式:
<FrameLayout
android:clickable="true"
android:id="@+id/progressContainer"
android:background="@color/alphaDisabled"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress"
android:layout_gravity="center"
android:layout_centerInParent="true"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
我正在使用容器,因为想要阻止点击屏幕上的任何Button
,而网络工作尚未完成。
我尝试只使用进度条(没有容器)。但在这种情况下,进度条缩放到整个屏幕。
使用framelayout + progressbar的方式是最好的方式(实现+性能)?
答案 0 :(得分:0)
您可以尝试使用'ProgressBarDialog'
这样的事情:
ProgressDialog progress = new ProgressDialog(this);
//Now you can set some properties of this dialog. Such as, its style, its text etc.
progress.setMessage("Uploading file: ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
现在您可以使用 progress.show()和 progress.dismiss()来显示/隐藏对话框
答案 1 :(得分:0)
在style.xml中创建自定义ProgressDialog主题
<style name="MyProgressDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:alertDialogStyle">@style/CustomAlertDialogStyle2</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
<style name="CustomAlertDialogStyle">
<item name="android:bottomBright">@android:color/transparent</item>
<item name="android:bottomDark">@android:color/transparent</item>
<item name="android:bottomMedium">@android:color/transparent</item>
<item name="android:centerBright">@android:color/transparent</item>
<item name="android:centerDark">@android:color/transparent</item>
<item name="android:centerMedium">@android:color/transparent</item>
<item name="android:fullBright">@android:color/transparent</item>
<item name="android:fullDark">@android:color/transparent</item>
<item name="android:topBright">@android:color/transparent</item>
<item name="android:topDark">@android:color/transparent</item>
</style>
现在使用透明背景创建具有此主题的进度对话框,并且不会破坏孔页...
// Initialize a new instance of progress dialog with custom theme
final ProgressDialog pd = new ProgressDialog(this, R.style.MyDialogTheme);
// Set progress dialog style spinner
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// Set the progress dialog background TRANSPARENT color
pd.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//Disable the dialog cancel on touch
pd.setCanceledOnTouchOutside(false);
pd.setIndeterminate(false);
// Finally, show the progress dialog
pd.show();
享受编码:)
答案 2 :(得分:0)
我当前的实现(我总是有根视图ConstraintLayout):
gcloud container get-credentials <cluster_name>
并添加到xml:
public class ScreenProgress extends FrameLayout {
public ScreenProgress(@NonNull Context context) {
super(context);
}
public ScreenProgress(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ScreenProgress(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (getChildCount() > 0) {
return;
}
ViewsUtils.setVisible(false, this);
bindContainer();
addProgressBar();
}
private void bindContainer() {
setClickable(true);
setBackgroundColor(ResourcesUtils.getColor(R.color.alphaDisabled));
ViewParent viewParent = getParent();
if (!(viewParent instanceof ConstraintLayout)) {
return;
}
ConstraintLayout parent = (ConstraintLayout) viewParent;
ConstraintSet set = new ConstraintSet();
set.clone(parent);
set.connect(getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
set.connect(getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
set.connect(getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set.connect(getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
set.constrainHeight(getId(), ConstraintSet.MATCH_CONSTRAINT);
set.constrainWidth(getId(), ConstraintSet.MATCH_CONSTRAINT);
set.applyTo(parent);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
setElevation(ResourcesUtils.getDp8());
}
}
private void addProgressBar() {
ProgressBar bar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleLarge);
FrameLayout.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
addView(bar, lp);
}
}