我遇到了一个奇怪的问题。我需要能够将 SearchView 小部件的提示文本垂直居中,以便它与放大镜和关闭图标对齐。
这是我迄今为止所掌握的内容的 fragment :
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,所以是的,它与提示文本相关联。”的评论之后。
真正的答案是,将文本围绕图像跨度对齐。这是有答案的!链接在这里:Align text around ImageSpan center vertical
关于java - 如何在不移动放大镜的情况下仅对齐 SearchView 的提示文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40711519/