【发布时间】:2016-05-17 10:34:34
【问题描述】:
我有两个相似的按钮
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/sync_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sync"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
作为项目添加到菜单
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/toggle_item"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/switch_button"
/>
<item
android:id="@+id/sync_item"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/sync"
/>
</menu>
我使用
在 onCreateOptionsMenu 中将项目膨胀到菜单getMenuInflater().inflate(R.menu.menu_main, menu);
一切似乎都很好,但是当我点击应用程序时,什么也没有发生,因为永远不会调用 onOptionsItemSelected(MenuItem item)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Integer test = item.getItemId();
switch (item.getItemId()) {
case R.id.toggle_item:
...
case R.id.sync_item:
...
return super.onOptionsItemSelected(item);
}
用
添加工具栏 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
【问题讨论】:
-
我试图在它旁边添加标准项目,这个项目在点击时触发事件,我的自定义带有定义的 actionLayout,它没有。
标签: android android-actionbar onclicklistener toolbar