Android LayoutInflater 的使用
LayoutInflater 有三種實現(xiàn)方法
1?LayoutInflater inflater = LayoutInflater.from(context) 這種方法常用在彈框?
2?LayoutInflater inflater = getLayoutInflater(); 這樣方法自己用在Fragment 以用布局
3?LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
下面已popwindow 為例來說下3中方法實現(xiàn)
第一種
public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;LayoutInflater inflater;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);inflater = LayoutInflater.from(this);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = inflater.inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}
不過一般都不是這樣寫的太麻煩了一般都是直接寫在一起的如下
public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = LayoutInflater.from(InflateActivity.this).inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}
第二種方法實現(xiàn)
public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = getLayoutInflater().inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}
第三種方法實現(xiàn)
public class InflateActivity extends AppCompatActivity {PopupWindow popupWindow;LayoutInflater inflater;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);inflater= (LayoutInflater)InflateActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {View view = inflater.inflate(R.layout.bottom__pop_item, null);popupWindow = new PopupWindow(view, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, true);popupWindow.setContentView(view);View rootview = LayoutInflater.from(InflateActivity.this).inflate(R.layout.inflate_activity_layout, null);popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}});}
}
簡單的會使用了在看看inflate 方法
當插入的是一個整體布局,比如彈框之類的
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {return inflate(resource, root, root != null);
}
resource 是布局,布局是一個整體,所以呢root 我們填寫為null
假如現(xiàn)在要一個布局里面插入另外一個布局 類似下面的
public class InflateActivity extends AppCompatActivity {private LinearLayout cons;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.inflate_activity_layout);cons = findViewById(R.id.cons);getLayoutInflater().inflate(R.layout.inflate_one_layout,cons,true);
}}
這個時候就是使用到了
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
這個root 就是父布局 ,attachToRoot 是,當講view 添加到父布局中的時候設(shè)置為true ,當不想添加如果已經(jīng)添加過了
設(shè)置為false ,,,那使用fragment 的時候為啥設(shè)置了false ,這個是因為fragment 里面有個什么方法會自動添加?
如果設(shè)置為true ,就是添加了2次 所以會報錯
?
?
總結(jié)
以上是生活随笔為你收集整理的Android LayoutInflater 的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android setAlpha 与 g
- 下一篇: Android Dialog 的使用总结