fred-ye / summary

my blog
43 stars 9 forks source link

[Android]当WebView碰上LinearLayout #32

Open fred-ye opened 9 years ago

fred-ye commented 9 years ago

直接看代码:

第一种方式:WebView外层套一个LinearLayout

Activity

public class TestWebViewActivity extends Activity {
    private LinearLayout llWraper;
    private WebView webView;
    private static final String TAG = "TestWebViewActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_webview);
        llWraper = (LinearLayout)findViewById(R.id.ll_wraper);
        webView = (WebView)findViewById(R.id.webView_test);
        new Handler().postDelayed(new GetMatricRunnable(), 5000);
    }

    class GetMatricRunnable implements Runnable {

        @Override
        public void run () {
            Log.i(TAG, "LinearLayout Width:" + llWraper.getWidth() + "  Height:" + llWraper.getHeight());
            Log.i(TAG, "WebView Width:" + webView.getWidth() + "  Height:" + webView.getHeight();
        }
    }

}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:my="http://schemas.android.com/apk/res/com.example.test"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/ll_wraper"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <WebView
            android:id="@+id/webView_test"
            android:layout_width="500dp"
            android:layout_height="match_parent" />
    </LinearLayout>

</RelativeLayout>
public class TestWebViewActivity extends Activity {
    private RelativeLayout rlWraper;
    private WebView webView;
    private static final String TAG = "TestWebViewActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_webview);
        rlWraper = (RelativeLayout)findViewById(R.id.rl_wraper);
        webView = (WebView)findViewById(R.id.webView_test1);
        new Handler().postDelayed(new GetMatricRunnable(), 5000);
    }
    class GetMatricRunnable implements Runnable {
        @Override
        public void run () {
            Log.i(TAG, "RelativeLayout Width:" + rlWraper.getWidth() + "  Height:" + rlWraper.getHeight());
            Log.i(TAG, "WebView Width:" + webView.getWidth() + "  Height:" + webView.getHeight());
        }
    }
}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/rl_wraper"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <WebView
            android:id="@+id/webView_test1"
            android:layout_width="500dp"
            android:layout_height="match_parent" />
    </RelativeLayout>

</RelativeLayout>

*运行结果如下:

screen shot 2014-11-02 at 7 12 01 pm

由此可见,若WebView的外层是LinearLayout时,WebView的宽度可以不受设备的实际宽度的影响,给它设置多大的值,它便能渲染成多大。若外层是RelativeLayout时,则会受设备的实际宽度的影响。这是为什么呢?

--》 To Be Continue