在adapter的getView中给view设置布局参数时,使用下面方法:
LayoutParams params = new LayoutParams( LayoutParams.FILL_PARENT, (int) (metrics.heightPixels * 0.23f + 0.5f)); convertView.setLayoutParams(params);
程序会出现错误
06-08 15:39:43.071: E/AndroidRuntime(610): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.setupChild(ListView.java:1688) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.makeAndAddView(ListView.java:1661) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.fillDown(ListView.java:637) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.fillFromTop(ListView.java:694) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.ListView.layoutChildren(ListView.java:1516) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.AbsListView.onLayout(AbsListView.java:1112) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1108) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.onLayout(LinearLayout.java:920) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.LinearLayout.onLayout(LinearLayout.java:918) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.View.layout(View.java:6569) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.ViewRoot.performTraversals(ViewRoot.java:979) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.view.ViewRoot.handleMessage(ViewRoot.java:1613) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:123) 06-08 15:39:43.071: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:4203) 06-08 15:39:43.071: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method) 06-08 15:39:43.071: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:521) 06-08 15:39:43.071: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 06-08 15:39:43.071: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
但最后设置如下时:
ViewGroup.LayoutParams param = convertView.getLayoutParams(); param.height = (int) (metrics.heightPixels * 0.20f + 0.5f); convertView.setLayoutParams(param);
就OK。
http://philn.blog.163.com/blog/static/104014753201101810573338/ 里也是一样的情况
给LinearLayout设置LayoutParams时用初始的方法则没有问题
暂时不知何解?