如何移除Android SearchView左侧的空白?

5
我正在尝试删除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

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


1
请发布您的 XML 文件。 - King of Masses
@GIBINTHOMAS那他应该在Search View右侧放置一个虚拟视图,宽度足够占用剩余空间吗?(简单的<view/>即可用于此)。 - samus
@LeandroDeMelloFagundes 为什么你选择不在左侧的剩余空间显示标题?没有标题看起来有点奇怪。 - BhalchandraSW
@BhalchandraSW 这只是一个网关屏幕。我真的不需要那里的标题。 - Leandro De Mello Fagundes
@BhalchandraSW 有一个标题在那里,是吗?嗯,这就是问题所在... - samus
显示剩余7条评论
4个回答

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>

我相信你的其他搜索界面代码应该可以工作。你只需要重定向你的事件调用。


可以尝试在清单文件中添加 **<activity android:theme="@android:style/Theme.NoTitleBar" />**。 - samus
这对我来说没有意义。我不是在谈论你的答案,而是我们做事情的方式。我已经将其开发到IOS平台上,非常容易。现在,我将不得不编写自己的搜索栏,因为Android不能让我填写我的搜索?奇怪:( - Leandro De Mello Fagundes
经过深思熟虑和花费了很多时间,我放弃了并接受了你的答案!谢谢 @BhalchandraSW - Leandro De Mello Fagundes

1
在活动布局中制作自定义搜索栏,并根据您的要求设置其高度和宽度。

BhalchandraSW提供了一个很好的例子...我会为你简洁而相关的建议点赞,因为你是对的(要么这样做,要么修改内置的SearchView https://dev59.com/HWgu5IYBdhLWcg3wK0Gr)? - samus

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>

0
  1. 获取SearchView的引用/句柄
  2. 然后使用getChildAt获取标题视图的引用(需要尝试,从0开始)
  3. 将TextView的可见性设置为gone。
View sv = findViewById(R.Id.SearchView);
View tv = sv.getChildAt(n);
tv.setVisibility(GONE);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接