setProgressBarIndeterminateVisibility(true) not working

后端 未结 1 1048
一向
一向 2020-12-08 12:24

I\'m trying to add a progress bar to my actionBar. I\'m talking about the spinning circle. I did a request and tried to set is vissible, but nothing happens. I have read man

相关标签:
1条回答
  • 2020-12-08 12:52

    First:

    Window provided Progress Bars are now deprecated with Toolbar.

    Second:

    you must use:

    setSupportProgressBarIndeterminateVisibility(true);
    

    instead of

    setProgressBarIndeterminateVisibility(true);
    

    because you extends ActionBarActivity. (because you are using supportRequestWindowFeature instead of requestWindowFeature)

    Third:

    If it crashes this is a known issue. If your library is updated sorry but it is now just a no-op:

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

    Fourth:

    My solution:

    use toolbar with progressBar widget:

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

    Layouts:

    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>
    

    and

    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>
    
    0 讨论(0)
提交回复
热议问题