召唤getLayoutInflater()在没有地方活动 [英] Call to getLayoutInflater() in places not in activity

查看:199
本文介绍了召唤getLayoutInflater()在没有地方活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有什么需要进口或者我怎么能叫的地方布局充气以外的活动?

What does need to be imported or how can I call the Layout inflater in places other than activity?

public static void method(Context context){
    //this doesn't work the getLayoutInflater method could not be found
    LayoutInflater inflater = getLayoutInflater();
    // this also doesn't work 
    LayoutInflater inflater = context.getLayoutInflater();
}

我能叫 getLayoutInflater 仅在活动中,是一种限制?如果我想创建自定义对话框,我想夸大查看它,或者,如果我想有吐司消息是从一个服务显示自定义视图,我只有从服务方面,我没有任何活动但我想显示自定义消息。

I am able to call getLayoutInflater only in activity, is that an restriction? What if I want to create custom dialog and I want to inflate view for it, or what if I want to have Toast message with custom view that is shown from a service, I only have the context from the service I do not have any activity but I want to show custom message.

我需要在code,是不是在活动类的地方充气。

I need the inflater in places in the code that isn't in the activity class.

我怎样才能做到这一点?

How can I do this ?

推荐答案

您可以使用此之外的活动 - 所有你需要的是提供一个上下文

You can use this outside activities - all you need is to provide a Context:

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

然后取回您的不同部件,你吹的布局:

Then to retrieve your different widgets, you inflate a layout:

View view = inflater.inflate( R.layout.myNewInflatedLayout, null );
Button myButton = (Button) view.findViewById( R.id.myButton );


修改为2014年七月

达维德的回答如何获得 LayoutInflater 实际上是比我更正确(这仍然是有效的,虽然)。

Davide's answer on how to get the LayoutInflater is actually more correct than mine (which is still valid though).

这篇关于召唤getLayoutInflater()在没有地方活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆