2014-10-09 61 views
1

你好每一個Android的怪胎,我已經使用LayoutInflater客戶我的Android ActionBar。爲什麼區別LayoutInflater導致差異SearchView圖標

,但我發現,差異LayoutInflater會導致產生差異搜索查看圖標

這是我的差異LayoutInflater和捕捉圖像

LayoutInflater mInflater1 =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
//You can change mInflater1 =LayoutInflater.from(this); is a same result 

LayoutInflater mInflater2 =LayoutInflater.from(getApplicationContext()); 

This is mInflater1 Result

This is mInflater2 Result

我的問題是,爲什麼差別LayoutInflater導致區別SearchView圖標?

+0

它是否在相同的操作系統版本? – Panther 2014-10-09 14:52:18

+0

是的是相同的操作系統,我的Android版本是4.3.1 – 2014-10-09 14:53:05

回答

1

那麼它的上下文決定從哪裏(主題)的圖標創建。我猜你的操作系統和應用程序有不同的主題 在這裏閱讀更多http://developer.android.com/reference/android/view/LayoutInflater.html

The Context in which this LayoutInflater will create its Views; most importantly, this supplies the theme from which the default values for their attributes are retrieved.

+0

謝謝,我認爲這是得到不同的主題圖標,對我有用:) – 2014-10-09 15:06:52

3

的問題是,在第二個例子中(我相信相關的第一張圖片),您使用的應用上下文膨脹視圖而不是活動上下文。

該框架做了很多工作來確保活動的上下文正確配置了正確的屏幕密度,主題和其他此類信息。應用程序不需要擔心顯示內容,因此它沒有配置爲這樣做。

不要使用應用程序上下文來膨脹視圖或獲取資源。使用活動上下文。