【发布时间】:2017-04-04 08:05:55
【问题描述】:
我手头有一个奇怪的问题。我需要能够垂直居中 SearchView 小部件的提示文本,以便它与放大镜和关闭图标对齐。
这是我目前所拥有的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