动态创建表格布局,根据屏幕大小自动调整单元格。

时间:2015-05-22 13:04:16

标签: android tablelayout

我试图以3x3矩阵格式在屏幕上显示9个图像,以便完美地适合屏幕。 我能够使用xml获得所需的结果,但我无法通过代码创建控件来复制相同的结果。 (3列适合我的屏幕,但行没有调整大小以适应屏幕) 请建议一种方法,通过该方法我可以通过代码创建动态控件并获得所需的结果。

注意:我希望所有内容都以矩阵格式放入我的屏幕,但没有滚动条。 (我无法使用网格布局实现此目的)

这是我的xml:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

    NSLog(@"-------------------------end.of.application.FBSDKApplicationDelegate");


    NSLog(@"facebook.Response=%@,url =%@,sourceApp= %@,annotation =%@",application,url,sourceApplication,annotation);


   return  [[FBSDKApplicationDelegate sharedInstance] application:application
                                                           openURL:url
                                                 sourceApplication:sourceApplication
                                                        annotation:annotation];








   // return true;

}




Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSData PF_dataFromBase64String:]: unrecognized selector sent to class 0x11343ca48'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000113193c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000112e2cbb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000011319afad +[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001130f113c ___forwarding___ + 988
    4   CoreFoundation                      0x00000001130f0cd8 _CF_forwarding_prep_0 + 120
    5   Chored                              0x000000010e2badf5 -[PFDecoder decodeDictionary:] + 658
    6   Chored                              0x000000010e2bb545 -[PFDecoder decodeObject:] + 130
    7   Chored                              0x000000010e2bb1b8 __30-[PFDecoder decodeDictionary:]_block_invoke + 94
    8   CoreFoundation                      0x0000000113192fbc ____NSDictionaryEnumerate_block_invoke421 + 28
    9   CoreFoundation                      0x0000000113092400 CFBasicHashApply + 128
    10  CoreFoundation                      0x00000001130cb85b __NSDictionaryEnumerate + 619
    11  Chored                              0x000000010e2bad5c -[PFDecoder decodeDictionary:] + 505
    12  Chored                              0x000000010e2bb545 -[PFDecoder decodeObject:] + 130
    13  Chored                              0x000000010e259631 -[PFCommandNetworkOperation _operationDidFinish] + 259
    14  CFNetwork                           0x000000011100c0bc __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 69
    15  CFNetwork                           0x000000011100c060 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 199
    16  CFNetwork                           0x000000011100c1c7 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 48
    17  CFNetwork                           0x0000000110edb757 ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 107
    18  CFNetwork                           0x0000000110fa8de1 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 273
    19  CFNetwork                           0x0000000110ec6a26 _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 72
    20  CoreFoundation                      0x000000011309a354 CFArrayApplyFunction + 68
    21  CFNetwork                           0x0000000110ec68e7 _ZN19RunloopBlockContext7performEv + 133
    22  CFNetwork                           0x0000000110ec6726 _ZN17MultiplexerSource7performEv + 256
    23  CFNetwork                           0x0000000110ec653c _ZN17MultiplexerSource8_performEPv + 72
    24  CoreFoundation                      0x00000001130c7431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    25  CoreFoundation                      0x00000001130bd2fd __CFRunLoopDoSources0 + 269
    26  CoreFoundation                      0x00000001130bc934 __CFRunLoopRun + 868
    27  CoreFoundation                      0x00000001130bc366 CFRunLoopRunSpecific + 470
    28  CoreFoundation                      0x000000011316a661 CFRunLoopRun + 97
    29  Foundation                          0x0000000112988383 __NSOQSchedule_f + 184
    30  libdispatch.dylib                   0x00000001141f2614 _dispatch_client_callout + 8
    31  libdispatch.dylib                   0x00000001141d96a7 _dispatch_queue_drain + 2176
    32  libdispatch.dylib                   0x00000001141d8cc0 _dispatch_queue_invoke + 235
    33  libdispatch.dylib                   0x00000001141dc3b9 _dispatch_root_queue_drain + 1359
    34  libdispatch.dylib                   0x00000001141ddb17 _dispatch_worker_thread3 + 111
    35  libsystem_pthread.dylib             0x000000011455f637 _pthread_wqthread + 729
    36  libsystem_pthread.dylib             0x000000011455d40d start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这会生成矩阵格式的输出,调整单元格以适应屏幕。

但是当我通过代码创建控件时,我无法复制相同的结果。这是我正在使用的代码:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone">
    <TableRow
        android:gravity="center"
        android:layout_weight="1">
        <ImageView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:src="@drawable/icon"
            android:layout_weight="1"
            android:layout_gravity="center"/>
        <ImageView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:src="@drawable/encrypt"
            android:layout_weight="1"
            android:layout_gravity="center"/>
        <ImageView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:src="@drawable/gear"
            android:layout_weight="1"
            android:layout_gravity="center"/>
    </TableRow>
    <TableRow
        android:gravity="center"
        android:layout_weight="1">
    .
    .//Same as row 1
    </TableRow>
    <TableRow
        android:gravity="center"
        android:layout_weight="1">
    .
    .//Same a row 1
    </TableRow>

    </TableLayout>

使用此代码,列会调整自身大小以适应屏幕,但行不会调整大小。

1 个答案:

答案 0 :(得分:1)

答案是LinearLayout和权重属性。 你可以:

  1. 动态添加LinearLayout&#39;水平&#39;。
  2. 添加3个LinearLayout s&#39; vertical&#39;到水平布局。
  3. 为每个垂直布局添加3个图像。
  4. 将每个垂直布局的权重设置为1。
  5. 将每张图片的重量设置为1。
  6. 注意:在API级别21或22之前,权重对网格布局不起作用,因此如果这是您的目标网格布局可能是要走的路。

    This explains creating layouts dynamically.