以编程方式从支持库创建GridLayout

时间:2012-08-10 16:03:48

标签: android grid-layout

在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)

1 个答案:

答案 0 :(得分:4)

事实证明,您需要将GridLayout支持库包含为Android库项目(apklib),而不是jar。我正在使用maven Android SDK deployer,但目前有一个issue对此公开。