【问题标题】:Removing extra space in Search View Icon in Actionbar删除操作栏中搜索视图图标中的多余空间
【发布时间】:2014-01-09 06:01:36
【问题描述】:

我正在定制 API 级别 11 及更高级别的 Android Actionbar。我有一个 Actionbar 的自定义视图,其中我有一个带有其他图标的 SearchView 小部件。但是当搜索视图是图标形状(不是展开形式)时,它占用的空间超过了预期的空间(如下图红色所示)。

如果我将我的图标更改为黑色图像,则会显示以下结果:

我已经完成了搜索视图的 xml 布局,并尝试通过访问搜索视图中的各个视图来减少此填充,但空间没有被删除。我为此编写的代码如下

 searchView.setBackgroundColor(Color.RED);
    searchView.setPadding(0, 0, 0, 0);

    searchView.setIconifiedByDefault(true);
    int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
    View searchPlate = searchView.findViewById(searchPlateId);
    searchPlate.setBackgroundColor(Color.BLUE);
    searchPlate.setPadding(0,0,0,0);

    int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    final TextView searchText = (TextView) searchPlate.findViewById(searchTextId);

    searchText.setPadding(0,0,0,0);
    searchText.setBackgroundColor(Color.GRAY);

    int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
    ImageView v = (ImageView) searchView.findViewById(searchImgId);
    v.setBackgroundDrawable(null);
    v.setPadding(0, 0, 0, 0);
    v.setImageResource(R.drawable.search);

我们可以通过任何方式做到这一点吗?我注意到搜索视图 xml 布局使用填充的一件事。这可能是原因吗?

我看过很多关于自定义操作栏和搜索视图的帖子,但没有人准确回答我的问题。

请提出建议。

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    可能是您的搜索图标图像有更多的宽度和高度。在任何图像编辑工具(如 Photoshop)中打开图像并查看图像大小并尝试减小图像的宽度和高度以使图像看起来更完美。

    更新:
    我认为这个问题背后的原因是 android:minWidth 的 ActionBar 按钮或图标的默认值是 56dip ,所以你需要改变它。

    希望对你有帮助!!

    【讨论】:

    • 感谢您的回复,但请查看我更新的帖子,当我更改图标以完成黑色图像时,差距仍然存在。所以我的图像比区域小很多。
    • 检查我更新的答案可能是你的问题的原因。
    • 谢谢..不客气..请将其标记为正确答案,以便它也可以帮助其他人。