Android 正确设置控件布局参数

 1.常见错误的设置
如果是mWebview对象的话,如果在xml文件已经定义了高度和长度
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,200);
mWebview.setLayoutParams(params);

运行之后出现log
W/dalvikvm(12945): threadid=1: thread exiting with uncaught exception (group=0x2aac8900)
E/AndroidRuntime(12945): FATAL EXCEPTION: main
E/AndroidRuntime(12945): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams
E/AndroidRuntime(12945):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:667)
E/AndroidRuntime(12945):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:319)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3133)
E/AndroidRuntime(12945):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1025)
E/AndroidRuntime(12945):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:394)
E/AndroidRuntime(12945):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:317)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3133)
E/AndroidRuntime(12945):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3133)
E/AndroidRuntime(12945):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3133)
E/AndroidRuntime(12945):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:539)
E/AndroidRuntime(12945):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:317)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3133)
E/AndroidRuntime(12945):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3133)
E/AndroidRuntime(12945):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:539)
E/AndroidRuntime(12945):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:317)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3133)
E/AndroidRuntime(12945):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
E/AndroidRuntime(12945):     at android.view.View.measure(View.java:8175)
E/AndroidRuntime(12945):     at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
E/AndroidRuntime(12945):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
E/AndroidRuntime(12945):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(12945):     at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(12945):     at android.app.ActivityThread.main(ActivityThread.java:4737)
E/AndroidRuntime(12945):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(12945):     at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(12945):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
E/AndroidRuntime(12945):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
E/AndroidRuntime(12945):     at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 1307):   Force finishing activity org.philn.yos.lezone/.MainActivity

2.正确设置控件的布局参数
        LayoutParams params = mWebViewHeader.getLayoutParams();
        params.height = height;
        params.width = LayoutParams.FILL_PARENT;       
        mWebViewHeader.setLayoutParams(params);

你可能感兴趣的:(Android 正确设置控件布局参数)