我尝试了一切,我在动作栏中看不到项目,有人能帮帮我吗?
这是我的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>
我只有一个&#34;黑色&#34; appbar,没关系,但我看不到它的内容。 我感谢任何帮助。 提前谢谢。
答案 0 :(得分:0)
尝试更改应用栏的颜色及其图标。
答案 1 :(得分:0)
啊搜索是一个动作VIew,你需要在onCreateOptionsMenu中初始化,以便将这些行添加到你的onCreateOptionsMenu函数
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
要将搜索操作显示为图标,请始终将showAsAction属性设置为始终如此
android:showAsAction="collapseActionView|always"