解决API<21时 GridLayout平均分配格行/列的问题

来源:互联网 发布:nginx 别名配置 编辑:程序博客网 时间:2024/06/08 14:21

问题:GridLayout在API21时引入了Android:layout_columnWeight和android:layout_rowWeight来解决平分问题,但是api21前怎么办呢?

解决:

1.引入兼容包:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. compile 'com.android.support:gridlayout-v7:23.0.0'  
2.为GridLayout设置权重:

app:layout_columnWeight app:layout_rowWeight (注意这是是app因为21以前android是没有这个属性的)

以及行列跨越:app:layout_rowSpan app:layout_columnSpan

eg:

执行效果:


代码:

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="com.nuctech.tr.trapp.activity.LoginActivity">  
  8.   
  9.     <com.zhy.android.percent.support.PercentLinearLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="match_parent"  
  12.         android:layout_centerInParent="true"  
  13.         android:gravity="center"  
  14.         android:orientation="vertical">  
  15.   
  16.         <LinearLayout  
  17.             android:layout_width="0dp"  
  18.             android:layout_height="0dp"  
  19.             android:orientation="vertical"  
  20.             app:layout_heightPercent="80%sh"  
  21.             app:layout_widthPercent="50%sw">  
  22.   
  23.             <com.nuctech.tr.trapp.ui.FillBlankView  
  24.                 android:layout_width="match_parent"  
  25.                 android:layout_height="60dp"  
  26.                 android:padding="5dp" />  
  27.   
  28.             <android.support.v7.widget.GridLayout  
  29.                 android:layout_width="match_parent"  
  30.                 android:layout_height="0dp"  
  31.                 android:layout_weight="1"  
  32.                 app:rowCount="4"  
  33.                 app:columnCount="4"  
  34.                >  
  35.                    
  36.   
  37.                 <Button  
  38.                     android:id="@+id/one"  
  39.                     app:layout_columnWeight="1"  
  40.                     android:layout_margin="2dp"  
  41.                     app:layout_rowWeight="1"  
  42.                     android:text="1" />  
  43.                     
  44.   
  45.                 <Button  
  46.                     android:id="@+id/two"  
  47.                     app:layout_columnWeight="1"  
  48.                     android:layout_margin="2dp"  
  49.                     app:layout_rowWeight="1"  
  50.                     android:text="2" />  
  51.                      
  52.   
  53.                 <Button  
  54.                     android:id="@+id/three"  
  55.                     app:layout_columnWeight="1"  
  56.                     android:layout_margin="2dp"  
  57.                     app:layout_rowWeight="1"  
  58.                     android:text="3" />  
  59.                      
  60.   
  61.                 <Button  
  62.                     android:id="@+id/delete"  
  63.                     app:layout_columnWeight="1"  
  64.                     android:layout_margin="2dp"  
  65.                     app:layout_rowWeight="1"  
  66.                     android:text="退格" />  
  67.   
  68.                 <Button  
  69.                     android:id="@+id/four"  
  70.                     app:layout_columnWeight="1"  
  71.                     android:layout_margin="2dp"  
  72.                     app:layout_rowWeight="1"  
  73.                     android:text="4" />  
  74.                     
  75.   
  76.                 <Button  
  77.                     android:id="@+id/five"  
  78.                     app:layout_columnWeight="1"  
  79.                     android:layout_margin="2dp"  
  80.                     app:layout_rowWeight="1"  
  81.                     android:text="5" />  
  82.                     
  83.   
  84.                 <Button  
  85.                     android:id="@+id/six"  
  86.                     app:layout_columnWeight="1"  
  87.                     android:layout_margin="2dp"  
  88.                     app:layout_rowWeight="1"  
  89.                     android:text="6" />  
  90.   
  91.                 <Button  
  92.                     android:id="@+id/confirm"  
  93.   
  94.                     app:layout_columnWeight="1"  
  95.                     android:layout_margin="2dp"  
  96.                     app:layout_rowSpan="3"  
  97.                     app:layout_rowWeight="3"  
  98.                     android:text="OK" />  
  99.   
  100.                 <Button  
  101.                     android:id="@+id/seven"  
  102.                     app:layout_columnWeight="1"  
  103.                     app:layout_gravity="fill"  
  104.                     android:layout_margin="2dp"  
  105.                     app:layout_rowWeight="1"  
  106.                     android:text="7" />  
  107.                     
  108.   
  109.                 <Button  
  110.                     android:id="@+id/eight"  
  111.                     app:layout_columnWeight="1"  
  112.                     android:layout_margin="2dp"  
  113.                     app:layout_rowWeight="1"  
  114.                     android:text="8" />  
  115.                     
  116.   
  117.                 <Button  
  118.                     android:id="@+id/nine"  
  119.                     app:layout_columnWeight="1"  
  120.                     android:layout_margin="2dp"  
  121.                     app:layout_rowWeight="1"  
  122.                     android:text="9" />  
  123.   
  124.                 <Button  
  125.                     android:id="@+id/removeall"  
  126.                     app:layout_columnWeight="1"  
  127.                     android:layout_margin="2dp"  
  128.                     app:layout_rowWeight="1"  
  129.                     android:text="X" />  
  130.                     
  131.   
  132.                 <Button  
  133.                     android:id="@+id/zero"  
  134.                     app:layout_columnWeight="1"  
  135.                     android:layout_margin="2dp"  
  136.                     app:layout_rowWeight="1"  
  137.                     android:text="0" />  
  138.                     
  139.   
  140.                 <Button  
  141.                     android:id="@+id/shutdown"  
  142.                     app:layout_columnWeight="1"  
  143.                     android:layout_margin="2dp"  
  144.                     app:layout_rowWeight="1"  
  145.                     android:text="S" />  
  146.             </android.support.v7.widget.GridLayout>  
  147.         </LinearLayout>  
  148.   
  149.   
  150.     </com.zhy.android.percent.support.PercentLinearLayout>  
  151.   
  152.   
  153. </RelativeLayout> 

转自:http://blog.csdn.net/lvwenbo0107/article/details/51769602


0 0
原创粉丝点击