如何删除Android搜索视图左侧的空间(不属于操作栏)?

问题描述 投票:5回答:6

我在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>
android layout padding searchview gravity
6个回答
11
投票

上面发布的图片链接不再有效,所以我假设这是我遇到的同样问题,如下图所示:

SearchView with padding lines

快速而肮脏的答案就是在你的SearchView上使用负左(/ start)填充,如下所示(警告:这是删除了SearchView的AppCompat 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"
  />

或者使用AppCompat SearchView,您可以在初始化期间使用app:layout=R.layout.some_custom_layout设置自定义布局,您可以在其中手动修改填充/边距。如果您打算采用这种方法,我建议您复制原始布局(found here)并只修改您需要的值,请参阅下面的示例:

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

我个人最后使用了首先提到的快速而肮脏的方法,我只想将其包含在内以便完整。

如果我错过任何东西,请打我。


5
投票
android:paddingStart="-16dp"

这对于棒棒糖后设备来说已经足够了。


3
投票

只需用你的SearchView作为输入来调用它,它将清除任何和所有插图。

private static void trimChildMargins(@NonNull ViewGroup vg) {
    final int childCount = vg.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = vg.getChildAt(i);

        if (child instanceof ViewGroup) {
            trimChildMargins((ViewGroup) child);
        }

        final ViewGroup.LayoutParams lp = child.getLayoutParams();
        if (lp instanceof ViewGroup.MarginLayoutParams) {
            ((ViewGroup.MarginLayoutParams) lp).leftMargin = 0;
        }
        child.setBackground(null);
        child.setPadding(0, 0, 0, 0);
    }
}

0
投票

嘿,在onCreate中动态设置SearchView的填充并尝试SearchView sv = findViewById(R.id.searchView1); sv.setPadding(0,2,0,0);


0
投票

尝试使用此代码一次

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">


    <SearchView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/searchView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
      />

    <TextView android:text="Hello World!" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_below="@id/searchView"/>

</RelativeLayout>

0
投票

以编程方式,您需要在SearchView中设置EditText的填充。

EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchEditText.setPadding(0, 2, 0, 2);

注意:将top,bottom和right的填充设置为您想要的值,但是对于leftPadding,0应该强制编辑文本中的所有内容都在SearchView的左侧


0
投票

试试这个:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp">

    <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:iconifiedByDefault="false"
        android:queryHint="Search"
       />

</android.support.v7.widget.Toolbar>
© www.soinside.com 2019 - 2024. All rights reserved.