如何删除Android searchview左侧(不属于Actionbar的部分)上的空间? [英] How to remove space on the left side of Android searchview (which is not part of actionbar)?

查看:84
本文介绍了如何删除Android searchview左侧(不属于Actionbar的部分)上的空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android应用中使用了searchview( searchview不是操作栏的一部分).我想删除搜索图标/searchview左侧的空间.我进行了很多搜索,收到了一些答案,这些答案适用于操作栏上的搜索视图.

我尝试使用 android:layout_gravity,android:gravity ,但它们似乎没有用.我曾考虑使用 android:contentInsetStart,android:contentInsetLeft ,但是这些选项不适用于searchview(这不是操作栏的一部分).

[ http://postimg.org/image/xz6pf8yp5/][1]

(由于我的声誉低于10,所以不能直接在此处发布图片)

serchview位于LinearLayout(垂直方向)中.这是我正在使用的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="2dp"
        android:layout_gravity="left"
        android:gravity="left"
        />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white" />

</LinearLayout>

解决方案

上面发布的图像链接不再起作用,因此我将假定这与我遇到的问题相同,如下图所示:

快速而肮脏的答案是在您的SearchView上使用负向左(/start)填充,如下所示(注意:这是删除了searchIcon的AppCompat SearchView).

<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"
  />

或者使用AppCompat SearchView,您可以将自定义布局设置为在初始化期间使用app:layout=R.layout.some_custom_layout进行夸大,您可以在其中手动修改填充/边距.如果您要采用此路线,建议您复制原始布局(在此处找到),然后修改所需的值,请参见以下示例:

<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"
  />

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>

我个人最终只是使用了前面提到的快速而又肮脏的方法,为了完整起见,我只是想将其包括在内.

如果我什么都错过了,打我一下.

I am using a searchview in android app (searchview is not a part of action bar). I want to remove the space on the left side of the search icon/ searchview. I searched a lot and received answers which worked for searchviews which are part of action bar.

I tried using android:layout_gravity, android:gravity, but they didnt seem to work. I thought of using android:contentInsetStart, android:contentInsetLeft, but these options are not available for searchview (which is not a part of action bar).

[http://postimg.org/image/xz6pf8yp5/][1]

(cant post image directly here since my reputation is less than 10)

The serchview is in LinearLayout (vertical orientation). Here is the code which I am using :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="2dp"
        android:layout_gravity="left"
        android:gravity="left"
        />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white" />

</LinearLayout>

解决方案

The image link posted above no longer works, so I am going to assume that this is the same problem I came across as illustrated in the image below:

The quick and dirty answer for this is to use negative left(/start) padding on your SearchView as demonstrated below (caveats: this is the AppCompat SearchView with the searchIcon removed).

<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"
  />

Alternatively with the AppCompat SearchView you can set a custom layout to be inflated during initialisation with app:layout=R.layout.some_custom_layout in which you can modify the paddings/margins manually. If you are going to take this route I recommend copying the original layout (found here) and just modifying the values you need, see example below:

<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"
  />

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>

I personally just ended up using the quick and dirty method mentioned first, I just wanted to include this for completeness.

Hit me up if I have missed anything.

这篇关于如何删除Android searchview左侧(不属于Actionbar的部分)上的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆