java - Android Actionbar 具有后退导航和下拉列表

标签 java android list android-actionbar

这就是我的问题。我需要自定义操作栏,以便操作栏中的 Logo 返回 Activity ,并通过按 Logo 附近打开下拉列表以更改 View 。我已经寻找解决方案几个小时但一无所获。这正是我想要得到的:

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;
}

}

最佳答案

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

要返回 Activity :Click here

祝你编码愉快!

关于java - Android Actionbar 具有后退导航和下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21062894/

相关文章:

java - 如何设置List<int[][]>中单元格的值?

android - 如何在没有样式工具栏的情况下动态更改所有工具栏的图标颜色

java - 返回android屏幕上opengl形状的像素坐标

python - 将列表转换为 NumPy 数组

java - 如何在Java中自定义Set的排序

java - Jersey REST 客户端 - 将自定义 MediaType 视为 MediaType.APPLICATION_JSON

java - 使用 String Wrapper 是更好的设计吗?

java - 使用 Retrofit with Field 传递参数

list - Kotlin 列出对象以列出 Id

python - 根据类别对列表进行分组