[英]Android Actionbar with both back navigation and dropdown list
SO here's my problem. 所以这是我的问题。 I need to customize my action bar in order that the logo from the action bar to go back on activity and by pressing near the logo to open a dropdown list in order to change the views.
我需要自定义操作栏,以使操作栏上的徽标恢复活动,并按徽标附近的按钮以打开下拉列表以更改视图。 I've searched for a solution for hours and i got nothing.
我已经搜索了一个小时的解决方案,但一无所获。 This is exactly what i'm trying to get:
这正是我想要得到的:
http://blog.mengto.com/wp-content/uploads/2013/02/android-actionbar.png http://blog.mengto.com/wp-content/uploads/2013/02/android-actionbar.png
PLEASE help me! 请帮我! Thanks
谢谢
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;
}
} }
You can add dropdown navigation as shown here: Click here 您可以添加下拉导航,如下所示: 单击此处
For going back to the activity: Click here 返回活动: 单击此处
Happy coding! 编码愉快!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.