带有后退导航和下拉列表的Android操作栏

时间:2014-01-11 13:35:46

标签: java android list android-actionbar

所以这是我的问题。我需要自定义操作栏,以便操作栏中的徽标返回活动,然后按标识附近打开下拉列表以更改视图。我已经搜索了几个小时的解决方案,但什么也没得到。这正是我想要的:

http://blog.mengto.com/wp-content/uploads/2013/02/android-actionbar.png

请帮帮我!感谢

import android.app.Activity;


public class Travel extends Activity {

String[] actions = new String[] { "Gallery View", "Grid View" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_travel);

    // action bar back button

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    /** Create an array adapter to populate dropdownlist */
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getBaseContext(),
            android.R.id.home, actions); //here i am trying to put the home button as the button, but basically i need only the logo to make the back action

    /** Enabling dropdown list navigation for the action bar */
    getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    /** Defining Navigation listener */
    ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {

        @Override
        public boolean onNavigationItemSelected(int itemPosition,
                long itemId) {
            Toast.makeText(getBaseContext(),
                    "This is the: " + actions[itemPosition],
                    Toast.LENGTH_SHORT).show();
            return false;
        }
    };

    /**
     * Setting dropdown items and item navigation listener for the actionbar
     * 
     * HERE IS WHERE THE CRASH OCCURS
     */
    //getActionBar().setListNavigationCallbacks(adapter, navigationListener);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.travel, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

您可以添加下拉导航,如下所示:Click here

返回活动:Click here

快乐的编码!