我正在尝试创建自定义搜索视图,以便在操作栏菜单项中单击搜索图标时显示。它可以工作,但当我打开它时,搜索栏顶部有额外的空间。有人可以告诉我我做错了什么。我也在下面发布代码。先感谢您。
主要活动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"
tools:context="com.abhi.searchview.MainActivity">
<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"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
Search_view.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@id/search_shadow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/shadow_overlay"
android:visibility="gone" />
<android.support.v7.widget.CardView
android:id="@id/search_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp"
app:cardCornerRadius="2dp"
android:maxHeight="0.9dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/search_height"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="start|center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@id/search_nav"
android:layout_width="56dp"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="@string/search"
android:focusable="true"
android:scaleType="centerInside" />
<android.support.v7.widget.AppCompatEditText
android:id="@id/search_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="start|center_vertical"
android:layout_weight="1"
android:background="@android:color/transparent"
android:ellipsize="end"
android:ems="10"
android:gravity="start|center_vertical"
android:imeOptions="actionSearch|flagNoExtractUi"
android:inputType="textNoSuggestions"
android:maxLines="1"
android:paddingEnd="16dp"
android:paddingStart="8dp"
android:privateImeOptions="nm"
android:textSize="16sp"
android:windowSoftInputMode="stateAlwaysHidden" />
<ImageView
android:id="@id/search_extra"
android:layout_width="48dp"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="@string/search"
android:focusable="true"
android:scaleType="center"
android:visibility="gone" />
<ImageView
android:id="@id/search_clear"
android:layout_width="48dp"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:contentDescription="@string/search"
android:focusable="true"
android:scaleType="center" />
</LinearLayout>
<View
android:id="@id/search_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
<android.support.v7.widget.RecyclerView
android:id="@id/search_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:visibility="gone" />
</LinearLayout>
</android.support.v7.widget.CardView>
MainActivity.kt
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
if (searchView == null)
searchView = bindSearchView(menu, R.id.action_search)
return true
}
答案 0 :(得分:0)