【发布时间】: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 布局使用填充的一件事。这可能是原因吗?
我看过很多关于自定义操作栏和搜索视图的帖子,但没有人准确回答我的问题。
请提出建议。
【问题讨论】: