本文介绍了Android 进度条未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该在 AsyncTask 中运行的进度条,但它没有出现,尽管任务正在运行

I have a progressbar that is supposed to run in an AsyncTask , but it is not appearing, although the task runs

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/splashportrait">
<ProgressBar android:layout_alignParentBottom="true" android:indeterminate="true"
    android:layout_centerHorizontal="true" android:paddingBottom="450dip"
    android:layout_width="200dip" android:layout_height="200dip"
    android:id="@+id/progressBar1"></ProgressBar>
</RelativeLayout>

代码:

ProgressBar diagProgress;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);

    diagProgress = (ProgressBar)findViewById(R.id.progressBar1);
    DiagnosticsTask diag = new DiagnosticsTask();
    diag.execute();

  /**rest of class ommitted here**/
}

private class DiagnosticsTask extends AsyncTask<String, String, Boolean> {

    //Show spinner
    protected void onPreExecute() {
        //dialog.setMessage("Loading corresponding destinations...");
        //dialog.show();
        diagProgress.setVisibility(View.VISIBLE);
        diagProgress.showContextMenu();
        Log.e("AsyncStatus", "spinner shown");
    }
 /*other parts of the thread ommitted here*/

}

推荐答案

试试用下面的替换你的 ProgressBar.

Try this replace your ProgressBar with the one below.

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:indeterminate="true"
    />

让我知道它是否有效,我会解释理由.

Let me know if it works, I would explain the rationale.

理由:现在我把你的代码放在下面的 ProgressBar

Rationale:Now I am putting your code below for ProgressBar

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="200dip"
    android:layout_height="200dip"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:indeterminate="true"
    android:paddingBottom="450dip"
    />

RelativeLayout 允许您进行 Z 排序.因此,由于您需要顶部的 ProgressBar,因此您无需进行您正在执行的操作.

RelativeLayout allows you Z-ordering. So since you needed ProgressBar on top you need not do the kind of manipulations you are doing.

android:layout_alignParentBottom="true"

这会在布局的底部设置进度条:

This sets the Progress Bar at the botton of the layout:

android:paddingBottom="450dip" android:layout_width="200dip" android:layout_height="200dip"

这里的所有三个值都是绝对的,就 Android 而言,这是一个严格的禁忌.很可能您的 paddingBottom 将您的 ProgressBar 推出了视图.由于您的填充大于控件的实际宽度/高度

All the three values here are absolute which is a strict no-no as far as Android is concerned. Most Likely your paddingBottom was pushing your ProgressBar out of View. As your padding is greater than the actual width/height of the control

作为经验法则,始终使用相对值使其适用于所有设备和外形尺寸.

As a thumb rule always use relative values for it to work on all the devices and form factors.

让我知道这是否有意义.

Let me know if this makes sense.

这篇关于Android 进度条未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 22:33