当前位置:   article > 正文

android:设置布局参数LayoutParams

android v.postinvalidate() 设置layoutparams代替

在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时用初始的方法则没有问题

 

 

 

暂时不知何解?

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/377315
推荐阅读
相关标签
  

闽ICP备14008679号