作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我开发了一个带有可扩展 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
中使用ActionBar
或ToolBar
来添加一个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/
我是一名优秀的程序员,十分优秀!