java - 我看不到应用栏中的项目

标签 java android xml appbar

我尝试了所有方法,但我在操作栏中看不到项目,有人可以帮助我吗?

这是我的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>

我只有一个“黑色”应用栏,没关系,但我看不到其中的项目。 我很感激任何帮助。 提前致谢。

最佳答案

ah search 是一个操作 VIew,您需要在 onCreateOptionsMenu 中初始化它,才能将这些行添加到您的 onCreateOptionsMenu 函数

MenuItem searchItem = menu.findItem(R.id.action_search); 
SearchView  searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

要将搜索操作显示为图标,请始终将 showAsAction 属性设置为始终如此

android:showAsAction="collapseActionView|always"

关于java - 我看不到应用栏中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35863197/

相关文章:

Java Tic Tac Toe 构造函数

java - 屏幕解锁后相机卡住

android - RelativeLayout -> match_parent 与父 View 大小不一样

xml - 使用 r httr 从谷歌搜索中抓取 url

sql-server - 具有多个命名空间的 SQL Server Xml 查询

java - 格式化字符串

java - JPA 使用 where 子句选择行

hibernate 中的 Java 枚举

android - AVD 屏幕方向不会随 CTRL+F11 或 CTRL+F12 改变

php - 不需要的字符串附加在 PHP SoapClient 中 SOAP 响应的开头和结尾