如何删除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的一部分)? 的相关文章

随机推荐

  • 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 不是操作栏的一部分 我想删除搜索图标 搜索视图左侧的空格 我搜索了很多并收到了适用于作为操作栏一部分的搜索视图的答案 我尝试使用机器人 布局重力 机器人 重力