![](/img/trans.png)
[英]how to set app:layout_columnWeight and app:layout_rowWeight to GridLayout's child programmatically
[英]How to support `layout_columnWeight` and `layout_rowWeight` in pre API 21?
我使用帶有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>
但它們僅適用於 v21 及更高版本。 如何在 API 21 之前支持layout_columnWeight
和layout_rowWeight
功能?
支持庫中的 GridLayout 版本向后兼容並支持這里提到的權重:
https://developer.android.com/reference/android/support/v7/widget/GridLayout.html
所以你只需要在你的 build.gradle 文件中添加compile 'com.android.support:gridlayout-v7:23.1.1'
並使用 support gridlayout ;)
在您的布局 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>
對於像我這樣的初學者來說, @Amir Ziarati 的答案是正確的。 您需要在build.gradle (Module:app) 中添加這一行
implementation 'com.android.support:gridlayout-v7:26.1.0'
像這樣
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'
}
然后使用
<android.support.v7.widget.GridLayout>
...</android.support.v7.widget.GridLayout>
而不是使用
android:layout_columnWeight="1"
android:layout_rowWeight="1"
使用:
app:layout_columnWeight="1"
app:layout_rowWeight="1"
這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.