2014-10-09 60 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

的问题是,在第二个例子中(我相信相关的第一张图片),您使用的应用上下文膨胀视图而不是活动上下文。

该框架做了很多工作来确保活动的上下文正确配置了正确的屏幕密度,主题和其他此类信息。应用程序不需要担心显示内容,因此它没有配置为这样做。

不要使用应用程序上下文来膨胀视图或获取资源。使用活动上下文。