getContentView().getLayoutParams()返回Android Marshmallow中的另一个类

getContentView().getLayoutParams()返回Android Marshmallow中的另一个类,android,Android,我有如下代码 .... PopupWindow popupWindow = new PopupWindow(someView); final WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) popupWindow.getContentView().getLayoutParams(); .... 问题popupWindow.getContentView().getLayoutParams返回不同的类,如

我有如下代码

....
PopupWindow popupWindow = new PopupWindow(someView);
final WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) popupWindow.getContentView().getLayoutParams();
....
问题popupWindow.getContentView().getLayoutParams返回不同的类,如下所示,因为我的类型转换失败

Marshamallow之前:android.View.WindowManager.LayoutParams

对于棉花糖:android.widget.FrameLayout.layoutParams


请问之前有没有人面对过这个问题或有什么解决方案?

没错。M将所有内容包装在名为
mDecorView
的框架布局中。如果深入研究PopupWindow源代码,您会发现类似于
CreatedDecorView(View-contentView)
的内容。
mDecorView
的主要目的是处理事件分派和内容转换,这对M来说是新的

要获取您的contentView,请尝试:

View container=(View)popupWindow.getContentView().getParent();
container.getLayoutParams()


请记住,如果contentView的背景不为null,它将嵌套两次。这意味着:
(视图)popupWindow.getContentView().getParent().getParent()

但我似乎在getParent()方法中找不到getLayourParams()?我无法获取它。。。getContentView().getParent()返回ViewParent。我看不出有一种方式可以像你提到的那样将其打印出来查看。我错过了什么吗?嗯,把它当作过去对我有用的观点。我现在没有M要测试,等我拿到它的时候再去看看。它可以很好的用typecast来查看。这就解决了我的问题。谢谢视图容器=(视图)弹出窗口。getContentView().getRootView();用这个。适用于Android M之前和之后(在果冻豆和棉花糖上测试)。