gpt4 book ai didi

android - 如何将 SearchView 添加到抽屉导航中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:20:11 25 4
gpt4 key购买 nike

我开发了一个带有可扩展 ListView 的抽屉导航,就像这样

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- The main content view -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/map" tools:context=".HomeActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />

<!-- The navigation drawer-->
<ExpandableListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"
android:listSelector = "@drawable/selector_list_item">
</ExpandableListView>
</android.support.v4.widget.DrawerLayout>

我想在抽屉导航中添加一个 SearchView,所以我将其添加到 ExpandableListView

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- The main content view -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/map" tools:context=".HomeActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />

<!-- The navigation drawer-->
<ExpandableListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"
android:listSelector = "@drawable/selector_list_item">
<SearchView android:id="@+id/sv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ExpandableListView>
</android.support.v4.widget.DrawerLayout>

我遇到了一个错误

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

谢谢

最佳答案

我认为在您的Activity 中使用ActionBarToolBar 来添加一个SearchView 将是一个很好的解决方案。你可以这样尝试:

首先,将 SearchView 添加到您的 Menu 中:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
yourapp:showAsAction="ifRoom|collapseActionView"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

其次,在您的 SearchView 上添加操作:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
// Configure the search info and add any event listeners
...
return super.onCreateOptionsMenu(menu);
}

而且,如果您想了解更多详细信息,请查看 here .

关于android - 如何将 SearchView 添加到抽屉导航中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30278916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com