2013-12-20 12 views
2

我正在API級別11和更高級別中定製Android操作欄。我有一個Actionbar的自定義視圖,其中有一個SearchView小部件和其他圖標。但是當搜索視圖處於圖標形狀(不是展開形式)時,它會佔用超過所需的空間(如下面的紅色所示)。在操作欄中刪除搜索視圖中的多餘空間

enter image description here

如果我改變圖標的​​黑色圖像下面的結果顯示:

enter image description here

我已經通過搜索視圖的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,這是爲什麼呢?

我已經看到很多自定義操作欄和搜索視圖的帖子,但沒有人確切地回答我的問題。

請建議。

回答

0

可能是您的搜索圖標圖像有更多的寬度和高度。在任何圖像編輯工具(如Photoshop)中打開圖像並查看圖像大小,並嘗試減小圖像的寬度和高度以及圖像看起來很完美。

更新:
我覺得後面這個問題的原因就是Android的默認值:minWidth的動作條按鈕或圖標是56dip,所以你需要去改變它。

我希望它會有幫助!

+0

感謝您的回覆,但請查看我更新的帖子,當我更改圖標以完成黑色圖像時,間隙仍然存在。所以我的圖像比這個區域小得多。 –

+0

檢查我的更新答案可能是你的問題的共鳴。 –

+0

感謝Vikas,這工作。 –