【发布时间】:2017-01-28 16:40:09
【问题描述】:
我想在单击工具栏图标时切换活动。
我的代码:
Toolbar toolbar = (Toolbar) findViewById(R.id.mytoolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
并像这样在点击时进行处理,但它不起作用:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
和 XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rel_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.project.www.simpleproject.activity.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/AppToolbar">
</android.support.v7.widget.Toolbar>
是否可以,如果可以,请告诉我谢谢
【问题讨论】:
-
在询问之前尝试解决方案
-
我试过了,但是没用。
-
什么不起作用
-
尝试添加
getSupportActionBar().setDisplayShowHomeEnabled(true); -
还是不行..
标签: android android-actionbar onclicklistener toolbar onitemclicklistener