上面发布的图像链接不再有效,因此我假设这与我遇到的问题相同,如下图所示:
对此的快速而肮脏的答案是在您的SearchView
如下所示(注意:这是 AppCompatSearchView
与searchIcon
已删除)。
<android.support.v7.widget.SearchView
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"
android:paddingLeft="-16dp"
android:paddingStart="-16dp"
app:iconifiedByDefault="false"
app:searchIcon="@null"
app:queryHint="Search"
app:theme="@style/ThemeOverlay.AppCompat.Dark"
/>
或者使用 AppCompatSearchView
您可以设置自定义布局在初始化期间膨胀app:layout=R.layout.some_custom_layout
您可以在其中手动修改填充/边距。如果您打算走这条路线,我建议复制原始布局(在这里找到 https://github.com/android/platform_frameworks_support/blob/master/v7/appcompat/res/layout/abc_search_view.xml)并只需修改您需要的值,请参见下面的示例:
<android.support.v7.widget.SearchView
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"
android:paddingLeft="-16dp"
android:paddingStart="-16dp"
app:iconifiedByDefault="false"
app:searchIcon="@null"
app:queryHint="Search"
app:theme="@style/ThemeOverlay.AppCompat.Dark"
app:layout="@layout/search_view"
/>
搜索视图.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
* Modified SearchView layout to remove 16dp left spacing from the input box.
*
* Original copyright notice:
*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<!-- This is actually used for the badge icon *or* the badge label (or neither) -->
<TextView
android:id="@+id/search_badge"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="2dip"
android:drawablePadding="0dip"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary"
android:visibility="gone"
/>
<ImageView
android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:contentDescription="@string/abc_searchview_description_search"
android:focusable="true"
style="?attr/actionButtonStyle"
/>
<LinearLayout
android:id="@+id/search_edit_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layoutDirection="locale"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/search_mag_icon"
android:layout_width="@dimen/abc_dropdownitem_icon_width"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:scaleType="centerInside"
android:visibility="gone"
style="@style/RtlOverlay.Widget.AppCompat.SearchView.MagIcon"
/>
<!-- Inner layout contains the app icon, button(s) and EditText -->
<LinearLayout
android:id="@+id/search_plate"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:orientation="horizontal"
>
<view
class="android.support.v7.widget.SearchView$SearchAutoComplete"
android:id="@+id/search_src_text"
android:layout_width="0dp"
android:layout_height="36dip"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@null"
android:dropDownAnchor="@id/search_edit_frame"
android:dropDownHeight="wrap_content"
android:dropDownHorizontalOffset="0dip"
android:dropDownVerticalOffset="0dip"
android:ellipsize="end"
android:imeOptions="actionSearch"
android:inputType="text|textAutoComplete|textNoSuggestions"
android:singleLine="true"
/>
<ImageView
android:id="@+id/search_close_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/abc_searchview_description_clear"
android:focusable="true"
android:paddingLeft="8dip"
android:paddingRight="8dip"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/submit_area"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/search_go_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/abc_searchview_description_submit"
android:focusable="true"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:visibility="gone"
/>
<ImageView
android:id="@+id/search_voice_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/abc_searchview_description_voice"
android:focusable="true"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:visibility="gone"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
我个人只是最终使用了首先提到的快速而肮脏的方法,我只是想将其包括在内以保持完整性。
如果我错过了什么,请联系我。