[英]How to support `layout_columnWeight` and `layout_rowWeight` in pre API 21?
I use the below grid layout with layout_columnWeight
and layout_rowWeight
to centralize my view in the grid cell.我使用带有
layout_columnWeight
和layout_rowWeight
的下面的网格布局来将我的视图集中在网格单元格中。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridLayout
android:id="@+id/container_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="3"
android:orientation="horizontal">
<View
android:id="@+id/view_red"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="center"
android:background="#ff0000"
android:layout_row="0"
android:layout_column="0" />
<View
android:id="@+id/view_green"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="center"
android:background="#00ff00"
android:layout_row="0"
android:layout_column="0" />
<View
android:id="@+id/view_blue"
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="center"
android:background="#0000ff"
android:layout_row="0"
android:layout_column="0" />
</GridLayout>
</RelativeLayout>
But they are for v21 and above only.但它们仅适用于 v21 及更高版本。 How to support
layout_columnWeight
and layout_rowWeight
feature in pre API 21?如何在 API 21 之前支持
layout_columnWeight
和layout_rowWeight
功能?
the version of GridLayout in support library is backward compatible and supports weights as mentioned here:支持库中的 GridLayout 版本向后兼容并支持这里提到的权重:
https://developer.android.com/reference/android/support/v7/widget/GridLayout.html https://developer.android.com/reference/android/support/v7/widget/GridLayout.html
so you just need to add compile 'com.android.support:gridlayout-v7:23.1.1'
to your build.gradle file and use support gridlayout instead ;)所以你只需要在你的 build.gradle 文件中添加
compile 'com.android.support:gridlayout-v7:23.1.1'
并使用 support gridlayout ;)
use it like below ( android.support.v7.widget.GridLayout
) in your layout xml file:在您的布局 xml 文件中像下面一样使用它(
android.support.v7.widget.GridLayout
):
<android.support.v7.widget.GridLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="3"
app:orientation="horizontal">
<View
android:id="@+id/view_red"
android:layout_height="100dp"
android:layout_width="100dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
app:layout_gravity="center"
android:background="#ff0000"
app:layout_row="0"
app:layout_column="0" />
</android.support.v7.widget.GridLayout>
For beginners like me @Amir Ziarati's answer is on point.对于像我这样的初学者来说, @Amir Ziarati 的答案是正确的。 you need to add this line in your build.gradle (Module:app)
您需要在build.gradle (Module:app) 中添加这一行
implementation 'com.android.support:gridlayout-v7:26.1.0'
Like this像这样
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:gridlayout-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
And then use然后使用
<android.support.v7.widget.GridLayout>
...</android.support.v7.widget.GridLayout>
Instead of Using而不是使用
android:layout_columnWeight="1"
android:layout_rowWeight="1"
Use:使用:
app:layout_columnWeight="1"
app:layout_rowWeight="1"
This will help.这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.