Action Bar icon as up enabled not the title

2019-03-13 12:42发布

How to make app icon as up enabled in actionbarsherlock (not the title only icon) like in whats app.

4条回答
我命由我不由天
2楼-- · 2019-03-13 13:00

My friend, I think this is Android version/build "feature", because I have two devices, (Nexus S and Nexus 7) (Android 4.1.2 and Android 4.2.2) and I am deploying the app I am developing on both devices, same exact code, on Nexus S the icon is "up", on Nexus 7 the icon and title are both "up".

查看更多
Bombasti
3楼-- · 2019-03-13 13:04

make sure your android:minSdkVersion="11" which could be seen in the manifest file, Up icon has been included from APK 11. Add the following to your onCreate method For home page put getActionBar().setDisplayHomeAsUpEnabled(false); make sure it false and on other activities you keep it enabled i,e "true". i have made a small sample plz try the below link which may be help you just import into your work space

http://www.mediafire.com/?hktdvts7yyduwv1

查看更多
时光不老,我们不散
4楼-- · 2019-03-13 13:10

The title is clickable together with the icon since Android 4.2.2. WhatsApp uses a custom view to display a two line title. This disables the title click along the way. You can do it the same way:

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.ab_title);

TextView title = (TextView) findViewById(android.R.id.text1);
title.setText("Title");

/res/layout/ab_title.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/TextAppearance.Sherlock.Widget.ActionBar.Title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:ellipsize="end"
    android:gravity="center_vertical" />
查看更多
闹够了就滚
5楼-- · 2019-03-13 13:12

Add the following to your onCreate method:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and define the following override method in your activity:

@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        onBackPressed();
        break;
    default:
        return super.onOptionsItemSelected(item);
    }
    return true;
}
查看更多
登录 后发表回答