setProgressBarIndeterminateVisibility(true) 不工作

setProgressBarIndeterminateVisibility(true) not working

我正在尝试向我的 actionBar 添加进度条。我说的是旋转圈。我做了一个请求并试图设置是可见的,但没有任何反应。 我已经阅读了很多可能的问题,但仍然无法弄清楚我做错了什么。

我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//Above setContentView, very important
    setContentView(R.layout.activity_main);
    //...other stuff
}

在创建时我没有调用的其他方法中。(这是一个 onClick 方法)

public void plus(View view){
    setProgressBarIndeterminateVisibility(true);//Nothing happens
    //...other stuff
}

我不明白哪里出了问题,请帮助我。

注意:我从未将其设置为 false

编辑: 我试过 mmlooloo 第二部分,但绝对注意到发生了。甚至第 3 部分都没有。所以我尝试了第 4 部分,但我给了我一个例外。

"This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead."

我删除了 Window.FEATURE_ACTION_BAR 请求,但它给了我同样的异常。 我不认为我需要将 windowActionBar 设置为 false,但我设置了并且它仍然给了我同样的异常。

还有其他选择吗?

第一个:

Window provided Progress Bars are now deprecated with Toolbar.

第二个:

你必须使用:

setSupportProgressBarIndeterminateVisibility(true);

而不是

setProgressBarIndeterminateVisibility(true);

因为您扩展了 ActionBarActivity。 (因为您使用的是 supportRequestWindowFeature 而不是 requestWindowFeature

第三个:

如果它崩溃,这是已知的 issue。 如果您的库已更新,抱歉,但它现在只是一个空操作:

setSupportProgressBarIndeterminateVisibility() crashing has been fixed for a future release, in that it will be a no-op.

第四个:

我的解决方案:

将工具栏与进度条小部件一起使用:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_spinner);
        progressBar.setVisibility(View.VISIBLE);
    }

布局:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar"/>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ProgressBar
        android:id="@+id/progress_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:indeterminate="true"
        android:visibility="gone" />

</android.support.v7.widget.Toolbar>