我手头有个奇怪的问题。我需要能够垂直居中的提示文本的SearchView小工具,使其与放大镜和关闭图标。
下面是我目前掌握的一些代码片段:
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。但这并没有带来任何结果。
发布于 2017-01-09 16:39:18
在中断这个项目几个月后,我觉得我应该回答我自己的问题。我问错了问题,在上面关于“放大镜是ImageSpan,所以是的,它绑定到提示文本”的评论之后,我应该意识到这一点。
对此的真正答案是,将文本围绕图像范围对齐。这个问题有一个答案!链接在这里:Align text around ImageSpan center vertical
https://stackoverflow.com/questions/40711519
复制相似问题