简体   繁体   中英

Home icon and up button separate in action bar

I am making an app for my school, and I finished a page in it. However, upon testing it, I found out that even after I put:

android.support.v7.app.ActionBar aB = getSupportActionBar();
        assert aB != null;
        aB.setDisplayHomeAsUpEnabled(true);
        // Makes App Icon Back/Up Button

        aB.setDisplayShowHomeEnabled(true);
        aB.setLogo(R.drawable.-omitted-);
        aB.setDisplayUseLogoEnabled(true);
        // Shows App Icon in Action Bar

The action bar displays the Up/back button as separate from the icon. The Up/back button is on the far left, the icon is in the middle and the Activity name to the far right. Clicking it does get me back to the home screen, but I want to display the Up/back button and icon together, like a group icon within the conversation in Whatsapp. This is my styles:

<style name="CredsStyle" parent="Theme.AppCompat.Light">
    <item name="android:windowActionBar">true</item>
    <!--Shows Action Bar-->

Is this because I have set the wrong parent style, the windowActionBar attribute or is it because I am using Build Tools/AppCompat 22 or did I forget another method or something I don't know and could you tell me what to do? Thanks for the help!

You can't call both aB.setDisplayShowHomeEnabled(true); and aB.setDisplayHomeAsUpEnabled(true); at the same instance. What you are trying to implement can be achieved like below:

getActionBar().setDisplayHomeAsUpEnabled(true);

This will not remove the app icon actually. and in order to add the click event on Home/Back Icon you have to override like below:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            /*NavUtils.navigateUpFromSameTask(this);
            Intent fgag = new Intent(getApplicationContext(), FragmentLauncher.class);
            startActivity(fgag);*/
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

I suggest you to use toolbar to display action bar , it is more easy and you can control it very easy , this is an example :

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/toolbar">

toolbar = (Toolbar) findViewById(R.id.header);
    setSupportActionBar(toolbar);
    toolbar.setLogo(R.drawable.best_deal);
    toolbar.setLogoDescription(R.string.logo_description);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM