问题:GridLayout在API21时引入了Android:layout_columnWeight和android:layout_rowWeight来解决平分问题,但是api21前怎么办呢?
解决:
1.引入兼容包:
- compile 'com.android.support:gridlayout-v7:23.0.0'
app:layout_columnWeight app:layout_rowWeight (注意这是是app因为21以前android是没有这个属性的)
以及行列跨越:app:layout_rowSpan app:layout_columnSpan
eg:
执行效果:
代码:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.nuctech.tr.trapp.activity.LoginActivity">
- <com.zhy.android.percent.support.PercentLinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_centerInParent="true"
- android:gravity="center"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:orientation="vertical"
- app:layout_heightPercent="80%sh"
- app:layout_widthPercent="50%sw">
- <com.nuctech.tr.trapp.ui.FillBlankView
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:padding="5dp" />
- <android.support.v7.widget.GridLayout
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- app:rowCount="4"
- app:columnCount="4"
- >
- <Button
- android:id="@+id/one"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="1" />
- <Button
- android:id="@+id/two"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="2" />
- <Button
- android:id="@+id/three"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="3" />
- <Button
- android:id="@+id/delete"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="退格" />
- <Button
- android:id="@+id/four"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="4" />
- <Button
- android:id="@+id/five"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="5" />
- <Button
- android:id="@+id/six"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="6" />
- <Button
- android:id="@+id/confirm"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowSpan="3"
- app:layout_rowWeight="3"
- android:text="OK" />
- <Button
- android:id="@+id/seven"
- app:layout_columnWeight="1"
- app:layout_gravity="fill"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="7" />
- <Button
- android:id="@+id/eight"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="8" />
- <Button
- android:id="@+id/nine"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="9" />
- <Button
- android:id="@+id/removeall"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="X" />
- <Button
- android:id="@+id/zero"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="0" />
- <Button
- android:id="@+id/shutdown"
- app:layout_columnWeight="1"
- android:layout_margin="2dp"
- app:layout_rowWeight="1"
- android:text="S" />
- </android.support.v7.widget.GridLayout>
- </LinearLayout>
- </com.zhy.android.percent.support.PercentLinearLayout>
- </RelativeLayout>
转自:http://blog.csdn.net/lvwenbo0107/article/details/51769602