Android 为什么不是';活动是否有getContentView()方法?
Android 为什么不是';活动是否有getContentView()方法?,android,android-layout,android-view,Android,Android Layout,Android View,Activity类有一个setContentView()方法。PopupWindow类有一个getContentView()方法,但其他方法没有。是否有其他方法可以获取活动的主内容视图?我也在寻找这个方法,但我只是认为在最外层的视图组中添加id可能更容易 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" andr
Activity
类有一个setContentView()
方法。PopupWindow
类有一个getContentView()
方法,但其他方法没有。是否有其他方法可以获取活动的主内容视图?我也在寻找这个方法,但我只是认为在最外层的视图组中添加id可能更容易
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/outer">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
不过,我会再找几分钟。我很喜欢它,因此我可以从最外层的布局使用它。我可以通过这个调用访问活动的内容:
ViewGroup view = (ViewGroup)getWindow().getDecorView();
您可能应该检查getDecorView是否在所有情况下都返回ViewGroup的instanceof,但如果活动中有LinearLayout,则上面的代码可以正常运行。要获得线性布局,您可以:
LinearLayout content = (LinearLayout)view.getChildAt(0);
如果你有这样一个函数:
void logContentView(View parent, String indent) {
Log.i("test", indent + parent.getClass().getName());
if (parent instanceof ViewGroup) {
ViewGroup group = (ViewGroup)parent;
for (int i = 0; i < group.getChildCount(); i++)
logContentView(group.getChildAt(i), indent + " ");
}
}
下面这行就可以了:
findViewById(android.R.id.content);
它本质上与相同(需要在活动的上下文中调用)
这也适用于findViewById。我没有意识到可以在xml的最外层标记中添加id。我是这样使用的:thisView=(View)findviewbyd(R.id.parent\u视图)+1使用的视图组视图=(视图组)getWindow().getDecorView();要通过调用view.removeAllViews()清除活动中的所有视图;-谢谢你的回答!
findViewById(android.R.id.content);
this.findViewById(android.R.id.content);