如何删除Android SearchView左侧空间?

时间:2017-03-28 13:49:43

标签: android android-appcompat searchview android-4.4-kitkat

我正在尝试在Android的搜索视图之前删除左侧空格,但没有成功。我尝试了我在互联网上找到的每个答案,但没有奏效。 我正在使用AppCompat主题进行此活动。我的目标是KitKat api(19)。

是的,有人能帮帮我吗? enter image description here

修改

活动布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/MainLayout"
android:background="#6ED19E">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/BackgroundLayout"
    android:background="#6ED19E">
    <ImageView
        android:src="@drawable/bgpattern"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/PatternImageView"
        android:scaleType="centerCrop" />
    <TextView
        android:text="Já estamos em muitas cidades\nEstamos na sua?"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/ManyCityLabel"
        android:textColor="#ffffff"
        android:textSize="25dp"
        android:width="20dp"
        android:gravity="center"
        android:textStyle="normal" />
</RelativeLayout>
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/LocationsListView"
    android:background="#ffffff"
    android:visibility="invisible" />

菜单

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
  <item android:id="@+id/LocationSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:showAsAction="always"
        tools:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>

我以编程方式执行的图标化内容,因为当我更改xml时无法正常工作

修改 如果我设置SearchView的背景颜色

enter image description here

搜索不适合整个空间。 为什么呢?

4 个答案:

答案 0 :(得分:1)

在活动布局中创建自定义搜索栏,并根据您

设置其高度和宽度

答案 1 :(得分:1)

Get output like this:

您可以执行以下操作:

隐藏onCreate()中的应用栏,如下所示:

// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.hide();
}

在布局文件中将背景设置为绿色(或您的颜色),添加SearchView并将SearchView的背景设置为白色,如下所示:

<android.support.constraint.ConstraintLayout 
    ...
    android:background="#00FF00"
    ...>

    <android.support.v7.widget.SearchView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFF" />
    ...
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    ....
</android.support.constraint.ConstraintLayout>

我很确定您的其他搜索界面代码可以正常运行。您只需要重定向事件调用。

答案 2 :(得分:0)

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/FullWidthSearchBar"
    android:background="#DD2277"
    android:orientation="horizontal">
    <item android:id="@+id/LocationSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:showAsAction="always"
        tools:actionViewClass="android.support.v7.widget.SearchView"/>
     <view android:layout_width="100dp"
        android:layout_height="wrap_content"/>
  </LinearLayout>
</menu>

答案 3 :(得分:0)

  
      
  1. 获取SearchView的引用/句柄
  2.   
  3. 然后使用getChildAt参考Title的视图(需要实验,从0开始)
  4.   
  5. 将TextView的可见性设置为已消失。
  6.   
View sv = findViewById(R.Id.SearchView);
View tv = sv.getChildAt(n);
tv.setVisibility(GONE);