LoginSignup
12
13

More than 5 years have passed since last update.

LayoutInflater の生成方法、View の inflate 方法比較

Last updated at Posted at 2015-08-04

TL;DR

どれも同じだから使えるの使え

context#getSystemService(Context.LAYOUT_INFLATER_SERVICE);

最もベース?の生成方法。

Layoutinflater.from(Context context)

これでも生成出来る。でもこれも中で何をやっているかというと、与えた context に対して上記の getSystemService() をコールしている為ほぼ同等。例外とか投げてくれるしキャストもやってくれるんでこれを使うのが一番妥当な気がする。

View.inflate(Context context, int resource, ViewGroup root)

こんなものもあり、与えた resource を直接 Inflater#inflate() してくれる。何をしているかというと、与えた context を引数に上記の Layoutinflater.from() を呼び出し以下略。
但し Inflater#inflate() する際に第 3 引数の無いメソッドをコールしている為、ArrayAdapter で getView をオーバーライドする際などに使用すると

addView(View, LayoutParams) is not supported in AdapterView

などと怒られることがある。こういったケースは素直に一旦 inflater を作成して、第三引数を false に指定したほうが良さそう。

12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13