【发布时间】:2016-06-22 03:38:33
【问题描述】:
我尝试了所有方法,但在我的操作栏中看不到项目,有人可以帮助我吗?
这是我的java代码:
public class mActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(myLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_frontend);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
Intent activity2 = new Intent(mActivity.this, LoginPage.class);
startActivity(activity2);
}
if (id == R.id.action_search) {
//toast
}
return super.onOptionsItemSelected(item);
}
}
这是我的 xml 代码:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#DDDDDD"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_frontend"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/right_drawer_frontend"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#DDDDDD"
android:layout_margin="5dp"
>
//...
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view_right_frontend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_right_customer"
app:menu="@menu/menu_right_customer" />
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.FloatingActionButton
//...
/>
</android.support.design.widget.CoordinatorLayout>
这是我的 xml 菜单代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_logout"
android:orderInCategory="100"
android:title="@string/action_logout"
app:showAsAction="never" />
<item android:id="@+id/action_search"
android:title="Search..."
android:icon="@drawable/ic_search_black_24dp"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
我只有一个“黑色”应用栏,没关系,但我看不到其中的项目。 我很感激任何帮助。 提前致谢。
【问题讨论】:
-
你为什么把
app:showAsAction作为never放在action_logout中? -
@Jas 表示它将在溢出点击时显示
-
@Jigen 你有没有尝试点击溢出图标,你看到菜单中的注销项了吗?
-
尝试将工具栏的
app:popupTheme属性更改为"ThemeOverlay.AppCompat.Light" -
哈哈,你有一个黑色的应用栏,你的搜索图标也是黑色的
android:icon="@drawable/ic_search_black_24dp"怎么会看到你的搜索图标,只需下载白色版本的搜索图标