【问题标题】:How do I align ONLY the hint text of a SearchView without moving the magnifying glass?如何在不移动放大镜的情况下仅对齐 SearchView 的提示文本?
【发布时间】:2017-04-04 08:05:55
【问题描述】:

我手头有一个奇怪的问题。我需要能够垂直居中 SearchView 小部件的提示文本,以便它与放大镜和关闭图标对齐。

This is what the SearchView currently looks like. Notice how the text view is vertically off centered in comparison to the magnifying glass and the x icon

这是我目前所拥有的sn-p:

SearchView sv = (SearchView) view.findViewById(R.id.search);

int id = sv.getContext().getResources().getIdentifier("android:id/search_src_text", null, null); 
int searchPlate = sv.getContext().getResources().getIdentifier("android:id/search_plate", null, null); 
int searchMagIcon = sv.getContext().getResources().getIdentifier("android:id/search_mag_icon", null, null); 
ImageView magIcon = (ImageView) sv.findViewById(searchMagIcon); View searchPlateView = sv.findViewById(searchPlate); 
EditText searchText = (EditText) sv.findViewById(id); searchText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);

//searchText.setHint("Some test hint text here"); 
searchPlateView.setBackgroundColor(Color.WHITE); 
searchText.setHintTextColor(Color.parseColor("#D8D8D8")); 
sv.setIconified(false); 
sv.clearFocus();

在注释掉的行中,我发现这与右侧的关闭图标完美对齐,但是,这摆脱了放大镜图标。我尝试过使用反射,这篇文章中概述了http://nlopez.io/how-to-style-the-actionbar-searchview-programmatically/

但是,这移动了提示文本和放大镜,并做了一些其他奇怪的事情。根据我的观察,似乎放大镜实际上与提示文本相关联,尽管在上面的代码中(我从 android 代码库中搜索)似乎放大镜是它自己的东西?我尝试过使用它,例如从放大镜 id 创建一个 ImageView。但是什么也没有发生。

【问题讨论】:

  • 放大镜是ImageSpan,所以是的,它与提示文本相关联。
  • 有什么方法可以做我所要求的吗?不使用放大镜移动提示文本?

标签: java android searchview


【解决方案1】:

我觉得我应该在这个项目中断几个月后回答我自己的问题。我问错了问题,应该意识到在上面关于“放大镜是 ImageSpan,所以是的,它与提示文本相关联”的评论之后。

对此的真正答案是,围绕图像跨度对齐文本。有一个答案!链接在这里:Align text around ImageSpan center vertical

【讨论】:

    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2013-01-03
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多