AdView和搜索视图无效

时间:2017-11-20 07:48:19

标签: android android-recyclerview admob ads searchview

您好我的应用程序没有显示之前显示的广告和搜索视图,但我创建了导航菜单,它不再显示它们。这是我的代码。我无法将其粘贴在此处,因为堆栈溢出不会让我

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>

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

1 个答案:

答案 0 :(得分:0)

尝试在SearchView

中添加Menu
<?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

image

<强> 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