当我调用getView()insiede一个片段时,它返回一个FrameLayout,但是片段根是RelativeLayout,怎么会发生这种情况呢?经过一些测试后,我还发现getView()返回不同的值,而不是onCreateView返回的值。
fragment_message.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="500dp"
>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray"/>
<ImageView
android:id="@+id/imageView1"
android:contentDescription="@string/hello_world"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:src="@drawable/seele" />
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/imageView1"
/>
</RelativeLayout>
MessageFragment.java:
public void onResume() {
RelativeLayout rl = relativeLayout;
WebView webView = (WebView) rl.getChildAt(rl.getChildCount() - 1);
webView.getSettings().setJavaScriptEnabled(true);
//getView();
//RelativeLayout rl2 = (RelativeLayout) getView();
//Toast.makeText(getActivity(), rl.getChildCount(), Toast.LENGTH_SHORT)
//.show();
FrameLayout fl = (FrameLayout) getView();
Toast.makeText(getActivity(), Integer.toString(((RelativeLayout)fl.getChildAt(0)).getChildCount()), Toast.LENGTH_SHORT).show();
//Toast.makeText(getActivity(), fl.getChildAt(0).getClass().toString(), Toast.LENGTH_SHORT).show();
//webView.loadUrl("http://www.google.com");
super.onResume();
}