Android 片段中的 getLayoutInflater()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26368178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:18:48  来源:igfitidea点击:

getLayoutInflater() in fragment

androidandroid-fragments

提问by udinsekomplek

I'm trying to show my results search in a ListViewon the Fragment, but it fails on:

我想表明我的成绩在搜索ListViewFragment,但它的失败:

LayoutInflater inflater = getLayoutInflater();

The method getLayoutInflater(Bundle) in the type Fragment is not applicable for the arguments ()

Fragment 类型中的 getLayoutInflater(Bundle) 方法不适用于参数 ()

This is my code:

这是我的代码:

public View getView(int position, View convertView, ViewGroup parent)
  {
   LayoutInflater inflater = getLayoutInflater();
   View row;

   row = inflater.inflate(R.layout.list_single, parent, false);

   TextView textview = (TextView) row.findViewById(R.id.TextView01);
   ImageView imageview = (ImageView) row
     .findViewById(R.id.ImageView01);

   textview.setText(data_text[position]);
   imageview.setImageResource(data_image[position]);

   return (row);

  }

How can I fix this?

我怎样才能解决这个问题?

回答by tyczj

if you are in a fragment you want

如果你在你想要的片段中

getActivity().getLayoutInflater();

or

或者

LayoutInflater.from(getActivity());

also you can do

你也可以

View.inflate();

or

或者

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

回答by chan sopheap

In Activity you can use like this:

在 Activity 中,您可以这样使用:

this.getLayoutInflater();

For Fragment you need to replace keyword "this" with "getActivity()"

对于 Fragment,您需要将关键字“this”替换为“getActivity()”

getActivity().getLayoutInflater();

Hope this will help!

希望这会有所帮助!

回答by Ahmed Hegazy

Now and don't know from when you can get it in your Fragment(support library v4) by

现在不知道从什么时候开始你可以在你的片段(支持库 v4)中得到它

getLayoutInflater(null);

回答by Hamza Mehmood

If you want to inflate layout (attach child layout to parent) in Fragment

如果你想膨胀布局(将子布局附加到父级) Fragment

Use this code

使用此代码

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

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

Now to Inflatechild layout

现在到Inflate子布局

View view = inflater.inflate(R.layout.layout_child, null);

Happy coding :)

快乐编码:)

回答by KRUPESH

Use it:

用它:

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