2
我正在API級別11和更高級別中定製Android操作欄。我有一個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佈局使用padding.Can,這是爲什麼呢?
我已經看到很多自定義操作欄和搜索視圖的帖子,但沒有人確切地回答我的問題。
請建議。
感謝您的回覆,但請查看我更新的帖子,當我更改圖標以完成黑色圖像時,間隙仍然存在。所以我的圖像比這個區域小得多。 –
檢查我的更新答案可能是你的問題的共鳴。 –
感謝Vikas,這工作。 –