在android中,是否可以完全以编程方式从支持库创建一个GridLayout(即不编写相应的xml)?
我在一个片段(称为 PromotionLayoutFragment)中创建我的 GridLayout,如下所示:
ViewGroup fragmentView = (ViewGroup)getView();
GridLayout gridLayout = new GridLayout(fragmentView.getContext());
gridLayout.setColumnCount(2);
gridLayout.setRowCount(15);
// add views to grid ...
fragmentView.addView(gridLayout);
当我使用 API 级别 14 中的 GridLayout 类时,这很好用,但是当我使用支持库(v7-r9)中的 GridLayout 时,我得到:
08-10 15:54:52.209: ERROR/AndroidRuntime(14687): FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: android.support.v7.gridlayout.R$dimen
at android.support.v7.widget.GridLayout.<init>(GridLayout.java:255)
at android.support.v7.widget.GridLayout.<init>(GridLayout.java:274)
at android.support.v7.widget.GridLayout.<init>(GridLayout.java:282)
at net.link.redbutton.fragment.PromotionLayoutFragment.showPromotions(PromotionLayoutFragment.java:168)
at net.link.redbutton.fragment.PromotionLayoutFragment.onImageResult(PromotionLayoutFragment.java:222)
at net.link.redbutton.fragment.SchedulerResponseFragment$1.onReceiveResult(SchedulerResponseFragment.java:37)
at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:43)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)