android:設定布局參數LayoutParams

來源:互聯網
上載者:User

在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$LayoutParams06-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時用初始的方法則沒有問題

 

 

 

暫時不知何解?

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.