最近寫了一個需求,點擊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); }