Android 2x2正方形网格布局
我想要一个2x2的网格布局,用正方形的CardView填充整个屏幕宽度 目前,我有这个 CardView正在拉伸到GridLayout的高度。如何使CardView的高度与其宽度相匹配,从而得到正方形 我当前的XML如下所示:Android 2x2正方形网格布局,android,xml,android-layout,Android,Xml,Android Layout,我想要一个2x2的网格布局,用正方形的CardView填充整个屏幕宽度 目前,我有这个 CardView正在拉伸到GridLayout的高度。如何使CardView的高度与其宽度相匹配,从而得到正方形 我当前的XML如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="htt
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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.example.jonas.trainingslog1.Activity_Settings"
android:orientation="vertical">
<GridLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:columnCount="2"
android:rowCount="2"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:layout_margin="16dp">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="16dp"/>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="16dp"/>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_margin="16dp"
android:layout_rowWeight="1" />
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="16dp"/>
</GridLayout>
我已经搜索了很多,但没有真正的帮助,欢迎任何意见 尝试将CardView宽度和高度设置为一些预期的相等值,例如200dp:
<GridLayout
android:layout_width="wrap_content"
android:layout_height="600dp"
android:columnCount="2"
android:rowCount="2"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:layout_margin="16dp">
<android.support.v7.widget.CardView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="16dp"/>
(...)
(...)
编辑:
或者尝试设置:
<GridLayout
android:layout_width="wrap_content"
试试看:
现在,您的网格具有匹配的父级宽度和600dp高度,其中的项目平均共享此区域。所以,如果你想要它们是正方形的,你需要一个正方形的网格。简单地说,您可以设置网格的宽度和高度,比如600dp。但当然,这不适合每个手机的屏幕。你需要动态计算这个尺寸。谢谢,我知道我可以将宽度和高度设置为常量,但正如你所说的,它不适合屏幕。Vishva Dave的回答在XmlThank内部解决了这个问题。谢谢,这是可行的,我不明白为什么,但GridLayout下的视图项使它能够工作。还有一件事,我注意到,当你将线性布局放入ScrollView时,它不再起作用了。你知道解决这个问题的方法吗?让它成为嵌套的scrollview而不是scrollview我不明白为什么,但是GridLayout下的视图项使它工作,因为它只是创建了一个虚拟空间。它被赋予了一定的重量,所以在所有的屏幕上,它都具有相同的高度。重量用于替代固定高度
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical">
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:columnCount="2"
android:rowCount="2"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:layout_margin="16dp">
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="16dp"/>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="16dp"/>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_margin="16dp"
android:layout_rowWeight="1" />
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="16dp"/>
</GridLayout>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"/>
</LinearLayout>