android - 如何删除Android SearchView 的剩余空间?

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

我试图删除 Android 搜索 View 之前的左侧空间,但没有成功。我尝试了在 Internet 上找到的所有答案,但没有用。 我正在为此 Activity 使用 AppCompat 主题。我的目标是 KitKat api (19)。

有人可以帮帮我吗? enter image description here

编辑

Activity 布局

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

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

最佳答案

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>

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

关于android - 如何删除Android SearchView 的剩余空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43071396/

相关文章:

工具栏内 SearchView 的 Android 数据绑定(bind)查询属性

android - Android 上的 GPS 和 WiFi 热点冲突

android - Theme.AppCompat.DayNight 不适用于 Android Lollipop

android - 是否可以在 Fragments 中搜索?

android - SwitchCompat 抛出错误

android - AppCompat 工具栏 : set toolbar theme from upper theme

android - SearchView 提示未显示输入的单个字符

android - 为什么 android 应用程序限制不适用于 google directions api?

java - 错误 : Gradle sync failed: Could not get unknown property 'API_KEY' for DefaultConfig_Decorated

java - BitmapFactory : Unable to decode stream: java. io.FileNotFoundException:打开失败:ENOENT(没有这样的文件或目录)