如何删除Android searchview左侧的空间(不属于actionbar的一部分)?

2023-12-27

我在 android 应用程序中使用 searchview (searchview 不是操作栏的一部分)。我想删除搜索图标/搜索视图左侧的空格。我搜索了很多并收到了适用于作为操作栏一部分的搜索视图的答案。

我尝试使用机器人:布局重力,机器人:重力,但它们似乎不起作用。我想用android:contentInsetStart, android:contentInsetLeft,但这些选项不适用于 searchview(它不是操作栏的一部分)。

[http://postimg.org/image/xz6pf8yp5/][1] http://postimg.org/image/xz6pf8yp5/%5D%5B1%5D

(不能直接在这里发布图片,因为我的声誉低于 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>

上面发布的图像链接不再有效,因此我假设这与我遇到的问题相同,如下图所示:

对此的快速而肮脏的答案是在您的SearchView如下所示(注意:这是 AppCompatSearchViewsearchIcon已删除)。

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

或者使用 AppCompatSearchView您可以设置自定义布局在初始化期间膨胀app:layout=R.layout.some_custom_layout您可以在其中手动修改填充/边距。如果您打算走这条路线,我建议复制原始布局(在这里找到 https://github.com/android/platform_frameworks_support/blob/master/v7/appcompat/res/layout/abc_search_view.xml)并只需修改您需要的值,请参见下面的示例:

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

搜索视图.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>

我个人只是最终使用了首先提到的快速而肮脏的方法,我只是想将其包括在内以保持完整性。

如果我错过了什么,请联系我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何删除Android searchview左侧的空间(不属于actionbar的一部分)? 的相关文章

  • 如何在android中点击画布上绘制的圆圈?

    我正在开发一个人脸检测应用程序 在这个应用程序中 我必须在脸上的眼睛和嘴巴用户可以点击拖动圆圈 在检测到的人脸上根据自己设置位置 因此 所有圆圈都已成功绘制在脸上 但我无法单击特定圆圈并使用缩小选项在整个脸上移动 请建议我有关相同问题的正确
  • 在phonegap中播放本地声音

    我有一个 wav文件在我的www文件夹 我正在使用 jQuery 和以下代码 警报响起 但声音不播放 难道我做错了什么
  • Android 构建 gradle 在特定设备上失败

    我面临一个奇怪的问题 当我编译我的应用程序以在 Android 7 0 的设备上运行它时 它可以工作 但是当我尝试为 Android 4 2 1 的设备进行编译时 它会失败并出现以下错误 错误 任务 app transformClasses
  • 如何检测android中的颠倒方向?

    在我的 Android 应用程序中 我有全景图像 并且我使用 TYPE ORIENTATION 传感器根据手机运动旋转该图像 它对于横向和纵向都工作良好 这是旋转逻辑的代码 Override public void onSensorChan
  • 服务在后台运行?

    我正在构建的应用程序的功能之一是记录功能 我通过在服务中启动 MediaRecorder 对象来实现此目的 Intent intent new Intent v getContext RecordService class Messenge
  • PhoneGap 是应用程序开发的好选择吗? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何在asp mvc的视图中加载选定的partialview?

    我正在从事 ASP NET Mvc 项目 我有一个类似于图像的视图 在此输入图像描述 https i stack imgur com xI6Fa jpg 我在布局中设计了右侧面板 我的布局代码 div class col md 3 pane
  • 将 ArrayList 保存在捆绑包 savingInstanceState 中

    ArrayList 是在类级别定义的 这些是我保存的实例方法 Override protected void onSaveInstanceState Bundle outState super onSaveInstanceState out
  • 如何在Firebase Android应用程序中分离两个不同的用户?

    我有一个应用程序 有两种不同类型的用户 一种是教师 第二种是普通用户 如果普通会员登录 他会去normal memberActivity如果他是教师会员 他会去Teacher memberActivity 我如何在登录活动中执行此操作 我的
  • 如何知道点击的widget id?

    我已经实施了一个widget与ImageButton and a TextView That ImageButton启动一个activity当它被点击时 这activity使用用户在活动上写入的内容更新小部件文本EditText 现在的问题
  • 如何在Android中隐藏应用程序标题? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我想隐藏应用程序标题栏 您可以通过编程来完成 import android app Activity import android os
  • 改造Android基本且简单的问题

    我的服务器返回简单的 Json 结果 如下所示 message Upload Success 我正在尝试将结果放入改造模型类中 public class MyResponse SerializedName message String me
  • 找不到资源矢量绘图的异常

    我将在某些设备上运行我的应用程序 其崩溃日志如下 01 04 16 54 02 206 7466 7466 com lawnmowers E AndroidRuntime FATAL EXCEPTION main Process com l
  • 使用 eclipse 配置mockito 时出现问题。给出错误:java.lang.verifyError

    当我将我的mockito库添加到类路径中 并使用一个简单的mockito示例进行测试时 我尝试使用模拟对象为函数add返回错误的值 我得到java lang verifyerror 以下是用于测试的代码 后面是 logcat Test pu
  • 在 Android SDK 中通过单击按钮更改背景颜色不起作用

    我有一个简单的程序 可以在单击按钮后更改背景颜色 但它不起作用 public class ChangeBackgroundActivity extends Activity Called when the activity is first
  • Exif 方向标签返回 0

    我正在开发一个自定义相机应用程序 我面临以下问题 当我尝试使用检索方向时ExifInterface 它总是返回 0 ORIENTATION UNDEFINED 这使我无法将图像旋转到正确的状态 从而无法正确显示 我使用示例代码来设置相机旋转
  • 如何用 XML 制作双渐变(类似 iphone)

    如何使用 XML 制作这种可绘制渐变 我可以做一个从颜色 A 到颜色 B 的简单渐变 但我不知道如何在同一个可绘制对象中组合两个渐变 我终于找到了一个带有图层列表的解决方案 这对我来说已经足够好了
  • TYPE_ACCELEROMETER 和 TYPE_LINEAR_ACCELERATION 传感器有什么区别?

    I think TYPE ACCELEROMETER显示设备加速 但是 我不明白什么时候应该使用TYPE LINEAR ACCELERATION 我需要计算移动设备的速度 哪种传感器适合此应用 另外 我读到TYPE LINEAR ACCEL
  • Android:列“_id”不存在

    我收到这个错误 IllegalArgumentException 列 id 不存在 当使用SimpleCursorAdapter从我的数据库中检索 该表确实有这个 id柱子 注意到这是一个常见问题 我尝试根据网上的一些解决方案来解决它 但它
  • Android 中带有无尽列表视图滚动的 AsyncTask

    我正在创建一个应用程序 其中我需要有无限的滚动列表视图 我不想在我的应用程序中使用任何库 我在网上看到了一些有助于实现此类列表视图的示例 但我的疑问是 当我的数据来自服务器并在异步任务中进行解析时 如何才能拥有无尽的列表视图 如何从滚动异步

随机推荐

  • MIPS 32位架构:如何在同一时钟周期内读取和写入寄存器文件中的寄存器?

    我的计算机体系结构书籍解释了这一点 由于对寄存器文件的写入是边沿触发的 因此我们的设计可以 在一个时钟周期内合法地读写同一个寄存器 read 将得到在较早的时钟周期写入的值 而 写入的值将可在后续时钟周期中读取 这是有道理的 我有点明白寄存
  • R 帮助页面中的“未运行”是什么意思?

    有时 在 R 帮助页面上 注释中会出现短语 未运行 从 with 的帮助页面查看这一点 Examples require stats require graphics examples from glm Not run library MA
  • 如何在Matlab绘图中插入两个X轴

    我想创建一个具有双 X 轴 m s 和 km h 且具有相同绘图的 Matlab 图 我已经找到了plotyy和 在Matlab存储库中 plotyyy 但我正在寻找 双 X 轴 下面一起来看看剧情吧 我的代码很简单 stem M 1 3
  • Android - 读取类内的 build.gradle 属性

    我希望能够访问 build gradle 属性 以便我可以自动化应用程序中的某些流程 这是我目前的结构 根项目 build gradle buildscript ext buildTools 25 0 2 minSdk 16 compile
  • iPhone模拟器中有切换深色模式和浅色模式的键盘快捷键吗?

    我使用 iPhone 模拟器进行开发 大多数 ui 组件都是使用 swift 编写的 有没有一种简单的方法可以在暗模式和亮模式之间切换以进行测试 我发现从设置中启用和禁用暗模式非常耗时 这个快捷方式将加快我对深色模式和浅色模式的测试 我最近
  • 发挥框架2.0的演变并创建触发器

    我一直在尝试使用 Play 2 0 3 创建此触发器 如果我手动运行它 它在 MySQL 中运行得很好 但当尝试从 Play 运行它时 它会失败 delimiter create trigger company updated before
  • Java 反射和重构中的痛苦

    Java Reflection 提供了一种在运行时自省对象的机制 毫不犹豫 这是一个很棒的功能 但它打破了所有重构约定 没有简单的方法 除了File Search 甚至在现代 IDE 中也能知道引用了哪个属性以及在哪里引用 这使得重构变得更
  • apache,允许文件名带有“?”

    我想制作网站的静态副本 保留现有的 URL 问题是 URL 看起来像 http mysite index php id XXX http mysite index php id XXX Apache 不想找到文件 index php id
  • 如何知道Android是否连接到WiFi或以太网? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 How to know whether I m connected to WiFi or ethernet in Android In
  • 使用一种编译器创建的静态 C 库是否与另一种编译器兼容?

    就我而言 我有一个使用代码源 gcc 构建的库 目标是arm cortex m4 然后我尝试将该库链接到使用 IAR 编译器编译的项目中 是否可以做到这一点 或者是否必须使用新工具重建库 哪些因素影响这个 静态库是几个目标文件的捆绑 这些目
  • Seaborn 散点图将气泡大小缩放为更大的点

    我正在关注这个example https seaborn pydata org generated seaborn scatterplot html我想创建更大的气泡 但无论我将尺寸列乘以多大 它们仍然很小 是否需要调整某种比例因子 我在文
  • 为什么这个 IFrame 没有占据完整的页面高度? [复制]

    这个问题在这里已经有答案了 我正在使用 Iframe 来显示 google com 或说任何网站 我使用了 height 100 但即便如此 我的 Iframe 大小也只是页面的一半 请让我知道为什么会发生这种情况 链接在这里 http j
  • Spark:测试 RDD 是否为空的有效方法

    没有一个isEmptyRDD 上的方法 那么测试 RDD 是否为空的最有效方法是什么 RDD isEmpty https issues apache org jira browse SPARK 5270将成为 Spark 1 3 0 的一部
  • 与PHP在同一页面显示结果

    我基本上是从 PHP 开始学习的 我想做的事情很简单 我想在同一页面显示计算结果 这是项目的结构 https i stack imgur com XGPVr png Code 索引 php header php
  • WAMP、Laravel 5:路由不起作用 - NotFoundHttpException

    我在本地计算机上使用 Laravel 5 和 WAMP 没有虚拟化 使用 Composer 创建一个名为 LaravelProject 的新项目后 我似乎根本无法让我的路线正常工作 我已经做了什么 Edited httpd conf to
  • 查找:显示模板速度

    我在新的 MVC4 站点上安装了 Mini Profiler 并注意到某些功能的等待时间很长Find DisplayTemplates包括字符串和日期时间 下面是一个例子 在另一个问题中 Sam Saffron 谈到了查找步骤 在随后的运行
  • 理解c++20中的convertible_to概念

    我对 C 20 概念仍然很陌生 我想知道为什么这不起作用 我想创建一个将数字连接为字符串的函数模板 所以我想尝试一些概念 我用了std convertible to检查输入的数据类型 即int在这种情况下 可以转换为std string 但
  • UICollectionView 标题宽度

    在我的 UICollectionViewFlowLayout 子类中 我有这个 self headerReferenceSize CGSizeMake 280 44 但是 标题的显示宽度为 320 这是集合视图的宽度 根据文档 这是正确的
  • 处理 Express 表单中的输入数组?

    假设我必须在同一页面上编辑一批相同类型的对象 jade form action method POST for each message id in messages ids input type text name message id
  • 如何删除Android searchview左侧的空间(不属于actionbar的一部分)?

    我在 android 应用程序中使用 searchview searchview 不是操作栏的一部分 我想删除搜索图标 搜索视图左侧的空格 我搜索了很多并收到了适用于作为操作栏一部分的搜索视图的答案 我尝试使用机器人 布局重力 机器人 重力