我正在尝试在 BaseAdapter 中使用 Viewgroup。这是我的代码
private ViewGroup createAnimLayout() {
ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
LinearLayout animLayout = new LinearLayout(con);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
但是我在 this.getWindow()
中遇到错误,例如“无法解析方法 getWindow()”。如何在 Baseadpater 中使用它。但它在 Activity 中起作用。
最佳答案
像这样在适配器中使用...
create Constructor of your Adapter and global variable of activity ....
private Activity ref;
public MyAdapter(Activity ref){
this.ref=ref;
}
并像这样使用......
private ViewGroup createAnimLayout() {
ViewGroup rootView = (ViewGroup) ref.getWindow().getDecorView();
LinearLayout animLayout = new LinearLayout(con);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
注意:- 使用context
代替this
....
关于java - 我无法在 Android 的 Baseadapter 中使用 viewgroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40435353/