最近写了一个需求,点击Dialog确定按钮,在弹出一个提示框或者是一个布局,因为显示的布局不是在Activity中创建,需要获得根布局将需要显示的布局添加上面显示出来,在写的时候遇到一个问题,自己定义的布局怎么都添加到布局里面,查看文档得知获取的根布局问题
刚开始这样写
private static View getRootView(context){
return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);}
后来改成这样就可以了.
private static View getRootView(Context context) { Activity activity = (Activity) context; return activity.getWindow().getDecorView().findViewById(android.R.id.content); }