How To Show and hide ActionBar with AppCompat v.7

前端 未结 9 1599
忘了有多久
忘了有多久 2020-11-27 16:06

I have a simple app that displays text.

The app starts with a main screen with a few options (ex. an info button that leads to info about the app, a browse button th

相关标签:
9条回答
  • 2020-11-27 16:25

    Extend you activity as AppCompatActivity and then use action bar as:-

    getSupportActionBar().hide();  // for hiding
    getSupportActionBar().show();  // for showing
    
    0 讨论(0)
  • 2020-11-27 16:27

    In onCreate method, after setContentView, use this simply to show or hide for each class.

    getSupportActionBar().hide();
    getSupportActionBar().show();
    

    If you want to set title,

    getSupportActionBar().setTitle("This is ToolBar");
    
    0 讨论(0)
  • 2020-11-27 16:29

    If you are trying to show/hide the ActionBar of an INTENT with AppCompatActivity and you get the following issues:

    • pointer null exception ...
    • thread issue ...

    Follow these steps to access to the same relevant thread into the intent:

    // 1) Calling the intent in the "main activity"
    Intent intent = new Intent(context, YourClass.class);
    startActivity(intent);
    
    // 2) Get the context in your "Intent activity"
    public class YourClass extends AppCompatActivity {
    YourClass mContext;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mContext = this;
    }
    
    // 3) Hide/Show Action bar from a method
    public void yourMethod(YourClass mContext){
    
        mContext.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mContext.getSupportActionBar().hide();
            }
        });
    
    }
    
    0 讨论(0)
  • 2020-11-27 16:30
    <style name="Theme.AppCompat.NoActionBar" parent="Theme.AppCompat.Light">
        <item name="android:windowActionBar">false</item>  
        <item name="android:windowNoTitle">true</item>
    </style>
    

    remove android in item windowActionbar.just like follow:

    <style name="Theme.AppCompat.NoActionBar" parent="Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>  
        <item name="android:windowNoTitle">true</item>
    </style>
    

    I hope it can help you.

    0 讨论(0)
  • 2020-11-27 16:30

    AppCompatActivity has its own embedded toolbar. You dont need to use extra toolbar definition in your main xml.

    just call getSupportActionBar().show()

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().show();
    }
    
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
    
            TextView text1=(TextView)findViewById(R.id.textView);
            text1.setText("I changed the text!");
    
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
  • 2020-11-27 16:36

    Dismiss ToolBar for all activities with AppTheme:
    styles.xml:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- .NoActionBar -->
    
    0 讨论(0)
提交回复
热议问题