Android 进度条不显示

发布于 2024-12-05 04:40:50 字数 1438 浏览 0 评论 0原文

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

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>

CODE:

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*/

}

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>

CODE:

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*/

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷夜 2024-12-12 04:40:50

当我在测试后忘记打开动画时遇到了这个问题 xD

I had that issue when I forgot to turn on animations after testing xD

属性 2024-12-12 04:40:50

尝试用下面的替换您的 ProgressBar。

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

让我知道它是否有效,我会解释原理。

理由:
现在我将您的代码放在下面,ProgressBarrelativeLayout

<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"
    />

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

android:layout_alignParentBottom="true"  

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

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

这里的所有三个值都是绝对值,这对于 Android 而言是严格禁止的。最有可能的是你的 paddingBottom 将你的 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.

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 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"

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.

没企图 2024-12-12 04:40:50

您忘记执行任务了吗?

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


  ....

Did you forget to execute your task?

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


  ....
柠檬色的秋千 2024-12-12 04:40:50

当我使可见性消失和可见瞬间消失使隐形在我的情况下工作不可见和可见时,我遇到了这个问题

private fun hideShowProgressBar(isShowProgress:Boolean){
    if(isShowProgress){
            progressBar.visibility = View.VISIBLE
            tvSetProgress.visibility = View.VISIBLE
    }else{
        progressBar.visibility = View.INVISIBLE
        tvSetProgress.visibility = View.INVISIBLE
    }
}

I had that issue when I make visibility gone and visible instant of gone make invisible in my case working invisible and visible

private fun hideShowProgressBar(isShowProgress:Boolean){
    if(isShowProgress){
            progressBar.visibility = View.VISIBLE
            tvSetProgress.visibility = View.VISIBLE
    }else{
        progressBar.visibility = View.INVISIBLE
        tvSetProgress.visibility = View.INVISIBLE
    }
}
烟沫凡尘 2024-12-12 04:40:50

我有这个问题。我通过进入 theme.xml 并撤消对主要品牌颜色所做的更改来解决这个问题。

当我将其改回这些颜色时,出现进度条:

<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>

I had this issue. I solved it by going into themes.xml and undoing the changes I made to the primary brand colors.

The progress bar appeared when I changed it back to these colors:

<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文