Using support action bar home enabled

后端 未结 5 2246
花落未央
花落未央 2021-02-08 13:31

I\'ve just modified our code to use the new SupportActionBar provided in the v7-appcompat library but when running the code on a Jellybean phone (presumably the same problem exi

相关标签:
5条回答
  • 2021-02-08 13:43

    This one worked for me:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your_activity);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // ... other stuff
    }
    
    @Override
    public boolean onSupportNavigateUp(){
        finish();
        // or call onBackPressed()
        return true;
    }
    

    The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.

    0 讨论(0)
  • 2021-02-08 13:54

    Try use Sherlock library for android devices such as Gingerbread cos android action bars is only supported from 3.0 upwards so the sherlock lock library gives you backward compatibility.

    http://actionbarsherlock.com/ --- download library here.

    Then add this lines in your code.

    ActionBar actionBar = getSupportActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
             actionBar.setIcon(android.R.color.transparent);
    
            actionBar.setDisplayUseLogoEnabled(false);
    

    This would help you to add a back home key in your action bar. It would also make your icon invisible if you dont want it to show. But if you want your app icon show on all activity simply comment this line below

    actionBar.setIcon(android.R.color.transparent);

    0 讨论(0)
  • 2021-02-08 13:54

    Now Please try this. Cause I was able to solve my own problem like this though it was on Sherlock. From your styles above I can see you did some customization to your themes. In my case I did some customization to my Sherlock theme and this was what gave me problem cos on android 4.0 and above my theme failed. so I simple added a piece of code that tells android to use the default Sherlock theme when it is running on android 4.0 and greater. So I suppose this would work for you. you tell android to use the default theme of v7-appcompat library on the version of android that is not working for you.

    Code is below:

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                this.setTheme(R.style.Theme_Sherlock);
            } else {
                this.setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
    
            }
    

    In your case edit the theme to v7-appcompat library.

    Please Mark as answer if it work for you. I believe it might be possible to customize the theme from the code for places were you are using this.

    0 讨论(0)
  • 2021-02-08 13:59

    You can add an ActionBar to your activity when running on API level 7 or higher by extending ActionBarActivity class for your activity and setting the activity theme to Theme.AppCompat or a similar theme.

    0 讨论(0)
  • 2021-02-08 14:06

    Have you tried using all three of these (also try swapping for getSupportActionbar())?

     getActionBar().setDisplayShowHomeEnabled(true);
     getActionBar().setHomeButtonEnabled(true);
     getActionBar().setDisplayHomeAsUpEnabled(true); 
    

    Have you tried handling the button manually?

    @Override
    public boolean onOptionsItemSelected(int featureId, MenuItem item) {
         int itemId = item.getItemId();
         if(itemId == android.R.id.home){
             // Do stuff
         }
         return true;
    }
    
    0 讨论(0)
提交回复
热议问题