AdView 和搜索视图不起作用

AdView and search view not working

你好,我的应用程序不显示广告和搜索视图,它以前显示过,但我创建了导航菜单,但它不再显示它们。这是我的代码。我无法将其粘贴到此处,因为堆栈溢出不允许我

MainActivity.java https://pastebin.com/zVQCQRAc

ActivityMain.xml https://pastebin.com/UUbStvE0

Appbar_main.xml https://pastebin.com/4tR6B0Hm

Content_main.xml https://pastebin.com/ipxy3gSU

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:background="#ededed"
    android:paddingBottom="10dp">


    <android.support.v7.widget.SearchView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/searchView"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/searchView"
        android:descendantFocusability="blocksDescendants"
        android:scrollbars="vertical"/>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner">
    </com.google.android.gms.ads.AdView>


</RelativeLayout>

此外,如果有人能告诉我如何在菜单栏中显示搜索,那将是一个巨大的帮助。 谢谢。

尝试在 Menu

中添加 SearchView
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu_search_album"
        android:icon="@drawable/ic_search"
        android:title="@string/str_search_album"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="collapseActionView|always" />

</menu>

而不是像这样在 res-XMl 文件夹中创建一个 Searchable.xml

Searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="@string/search_trips"
    android:label="@string/app_name" />

然后将其添加到您的清单文件中

    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />

将此代码添加到您的 Activity.java

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_album_search, menu);

        final MenuItem item = menu.findItem(R.id.menu_search_album);
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);

        searchView.setOnQueryTextListener(this);
        return true;

    }

更多信息How to Create a Search Interface