2

I am confused the way to set home icon on ActionbarSherlock and of course am new to this ActionBarSherlock. Have checked many sources, but unable to get how to set the home icon. Below is my class that sets the ActionbarSherlock.

public abstract class BaseActivity extends SherlockActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem miPrefs = menu.add("Login");
        miPrefs.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        miPrefs.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent loginIntent = new Intent(BaseActivity.this, LoginForm.class);
                startActivity(loginIntent);
                return true;
            }
        });
        return true;
    }
}

Of course I know how to set application icon as home icon in the normal action bar. The following is the way I usually set the normal actionbar.

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuItem menu1 = menu.add(0, 0, 0, "Login");
    menu1.setIcon(R.drawable.image1);
    menu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}

In the onCreate(), we have to get the actionbar by getActionBar() and then with actionbar.setDisplayHomeAsEnabled(true), it is possible to set the application icon as home icon. By setting the following we can listen to the clicks of home icon.

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case android.R.id.home:
        // Here we can keep the code to get to the mainactivity.
        return true;
    }
}

Also, when I just try to get the actionbar by ActionBar actionbar = getSupportActionBar(); in oncreate(), I get this error,

Type mismatch: cannot convert from com.actionbarsherlock.app.ActionBar to android.app.ActionBar

I'm confused about how to set the application icon as home icon based on the above code of ActionbarSherlock and listen for the clicks. How can I get that done?

4

1 に答える 1

2

ActionBar でアプリ アイコンをクリックできるようにする (ABS を使用)

@Override
public void onCreate() {
    super.onCreate();
    getSupportActionBar().setHomeButtonEnabled(true);
}

getActionBar()ABS はライブラリであるため、その機能にアクセスする場合は、デフォルトの Android メソッド/クラス ( やなど)と混同しないように、独自のメソッド/クラスを使用する必要がありますgetSupportActionBar()。サンプル コードの最適な場所はhttps://github.com/JakeWharton/ActionBarSherlock/tree/master/samples/demosです。

クリック音を聞く

上記のものと同じです。

于 2012-12-07T16:12:17.073 に答える