2016-02-29 4 views
3

J'utilise un searchview dans l'application Android (searchview ne fait pas partie de la barre d'action). Je veux supprimer l'espace sur le côté gauche de l'icône de recherche/searchview. J'ai beaucoup cherché et j'ai reçu des réponses qui ont fonctionné pour les searchviews qui font partie de la barre d'action.Comment faire pour supprimer l'espace sur le côté gauche de la recherche Android (qui ne fait pas partie de la barre d'action)?

J'ai essayé d'utiliser android: layout_gravity, android: gravité, mais ils ne semblaient pas fonctionner. J'ai pensé à utiliser android: contentInsetStart, android: contentInsetLeft, mais ces options ne sont pas disponibles pour searchview (qui ne fait pas partie de la barre d'action).

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

(cant post image directement ici depuis ma réputation est inférieure à 10)

Le serchview est en LinearLayout (orientation verticale). Voici le code que j'utilise:

<?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> 
+0

Je suis un peu en retard à celui-ci mais si vous n'avez pas résolu cela, consultez ma réponse ci-dessous. – Bulwinkel

Répondre

0

Hey pour définir le remplissage SearchView dynamiquement onCreate et essayer SearchView sv = findViewById (R.id.searchView1); sv.setPadding (0,2,0,0);

+0

Hey j'ai essayé de définir le remplissage par programme, mais ça ne fonctionne pas. Je suppose qu'il y a un problème avec le searchview lui-même. Je suppose que EditText aurait fonctionné dans ce cas. – coolcoder

0

Essayez d'utiliser ce code une fois

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

Est-ce que cela fait partie de votre réponse précédente? Ou une réponse entièrement nouvelle? Ne publiez pas plusieurs réponses à moins qu'elles offrent des solutions différentes. –

+0

Oui, c'est différent de la réponse précédente –

+0

Hé, j'ai essayé ça. Cela n'a pas fonctionné .. – coolcoder

7

Le lien d'image affiché ci-dessus ne fonctionne plus, donc je vais supposer que ce soit le même problème, je suis tombé comme illustré dans la image ci-dessous:

SearchView with padding lines

La réponse rapide et sale pour t son est d'utiliser le remplissage gauche gauche (/ start) sur votre SearchView comme démontré ci-dessous (avertissements: ceci est le AppCompat SearchView avec le searchIcon enlevé).

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

Différemment avec AppCompat SearchView vous pouvez définir une mise en page personnalisée à gonfler lors de l'initialisation avec app:layout=R.layout.some_custom_layout dans lequel vous pouvez modifier les rembourrages/marges manuellement. Si vous allez prendre cette route, je recommande de copier la mise en page originale (found here) et juste modifier les valeurs dont vous avez besoin, voir exemple ci-dessous:

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

Personnellement J'ai fini par utiliser la méthode rapide et sale mentionnée en premier, je voulais juste inclure ceci pour l'exhaustivité.

Frappe-moi si j'ai raté quoi que ce soit.

3

Il suffit d'appeler cela avec votre SearchView comme entrée et il effacera tous les encarts.

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

Programatically, vous devez définir le remplissage de l'EditText dans le SearchView.

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

Remarque: rembourrage fixé pour haut, en bas, et le droit à la valeur que vous s'il vous plaît, mais 0 pour leftPadding devrait forcer tout le texte d'édition à la gauche du SearchView

1
android:paddingStart="-16dp" 

C'est assez pour les dispositifs post sucette.

+1

Cela fonctionne pour moi quand searchview est dans la barre d'outils et a un remplissage de 8dp. Donc j'utilise 'android: paddingStart =" - 8dp "' –