Android 如何使 GridLayout 适合屏幕尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10347846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a GridLayout fit screen size
提问by rex
The GridLayout
API docs is very difficult to learn ......
is there any one who can teach me how to set child View
s to have something like "weight" of LinearLayout
?
该GridLayout
API文档是非常难学......
有没有谁可以教我怎么一组孩子的任何一个View
s到有类似的“权重” LinearLayout
?
Right now it looks like all are placed on the left hand side,
I have tried many times and still can not make it like each one the half width of the screen.
现在看起来好像都放在左边,
我试了很多次,仍然不能让它像屏幕的一半宽度一样。
Edited : I don't know what can I do for when the childen are all wrap_content
......
even if I want to set some image in specific size , this class helps me to make ImageView
wrap_content
.........it cannot operate normal , did I miss some setting ?!?
编辑:我不知道当孩子们都wrap_content
...的时候我能做什么......即使我想设置一些特定尺寸的图像,这个课程也帮助我制作ImageView
wrap_content
......它无法正常运行,我错过了一些设置吗?!?
回答by MH.
Note: The information below the horizontal line is no longer accurate with the introduction of Android 'Lollipop' 5, as GridLayout
doesaccommodate the principle of weights since API level 21.
注意:随着 Android 'Lollipop' 5 的引入,水平线下方的信息不再准确,因为GridLayout
它适应了 API 级别 21 以来的权重原则。
Quoted from the Javadoc:
引用自Javadoc:
Excess Space Distribution
As of API 21, GridLayout's distribution of excess space accomodates the principle of weight. In the event that no weights are specified, the previous conventions are respected and columns and rows are taken as flexible if their views specify some form of alignment within their groups. The flexibility of a view is therefore influenced by its alignment which is, in turn, typically defined by setting the gravity property of the child's layout parameters. If either a weight or alignment were defined along a given axis then the component is taken as flexible in that direction. If no weight or alignment was set, the component is instead assumed to be inflexible.
Multiple components in the same row or column group are considered to act in parallel. Such a group is flexible only if all of the components within it are flexible. Row and column groups that sit either side of a common boundary are instead considered to act in series. The composite group made of these two elements is flexible if one of its elements is flexible.
To make a column stretch, make sure all of the components inside it define a weight or a gravity. To prevent a column from stretching, ensure that one of the components in the column does not define a weight or a gravity.
When the principle of flexibility does not provide complete disambiguation, GridLayout's algorithms favour rows and columns that are closer to its right and bottom edges. To be more precise, GridLayout treats each of its layout parameters as a constraint in the a set of variables that define the grid-lines along a given axis. During layout, GridLayout solves the constraints so as to return the unique solution to those constraints for which all variables are less-than-or-equal-to the corresponding value in any other valid solution.
超空间分布
从 API 21 开始,GridLayout 的多余空间分布符合权重原则。在未指定权重的情况下,如果列和行的视图在其组内指定了某种形式的对齐方式,则将遵守先前的约定,并且将列和行视为灵活。因此,视图的灵活性受其对齐方式的影响,而对齐方式通常通过设置子布局参数的重力属性来定义。如果沿给定轴定义了权重或对齐方式,则组件在该方向上被视为灵活。如果未设置权重或对齐方式,则假定组件是不灵活的。
同一行或列组中的多个组件被视为并行操作。只有当其中的所有组件都是灵活的时,这样的组才是灵活的。位于公共边界两侧的行组和列组被视为串联操作。如果由这两个元素组成的复合组中的一个元素是柔性的,则它是柔性的。
要使柱子拉伸,请确保其中的所有组件都定义了重量或重力。为防止列拉伸,请确保列中的组件之一未定义重量或重力。
当灵活性原则不能完全消除歧义时,GridLayout 的算法偏爱靠近其右边缘和下边缘的行和列。更准确地说,GridLayout 将其每个布局参数视为一组变量中的约束,这些变量定义了沿给定轴的网格线。在布局期间,GridLayout 解决约束,以便为所有变量小于或等于任何其他有效解决方案中的相应值的那些约束返回唯一解决方案。
It's also worth noting that android.support.v7.widget.GridLayout
contains the same information. Unfortunately it doesn't mention which version of the support library it was introduced with, but the commit that adds the functionalitycan be tracked back to July 2014. In November 2014, improvements in weight calculation and a bug was fixed.
还值得注意的是,android.support.v7.widget.GridLayout
包含相同的信息。不幸的是,它没有提到它是用哪个版本的支持库引入的,但是添加功能的提交可以追溯到 2014 年 7 月。 2014 年 11 月,权重计算的改进和错误得到了修复。
To be safe, make sure to import the latest version of the gridlayout-v7 library.
为安全起见,请确保导入最新版本的 gridlayout-v7 库。
The principle of 'weights', as you're describing it, does not exist with GridLayout
. This limitation is clearly mentioned in the documentation; excerpt below. That being said, there are some possibilities to use 'gravity' for excess space distribution. I suggest you have read through the linked documentation.
正如您所描述的那样,“权重”原则不存在于GridLayout
. 文档中清楚地提到了这个限制;摘录如下。话虽如此,有一些可能性可以使用“重力”来分配多余的空间。我建议您通读链接的文档。
Limitations
GridLayout does not provide support for the principle of weight, as defined in weight. In general, it is not therefore possible to configure a GridLayout to distribute excess space in non-trivial proportions between multiple rows or columns. Some common use-cases may nevertheless be accommodated as follows. To place equal amounts of space around a component in a cell group; use CENTER alignment (or gravity). For complete control over excess space distribution in a row or column; use a LinearLayout subview to hold the components in the associated cell group. When using either of these techniques, bear in mind that cell groups may be defined to overlap.
限制
GridLayout 不支持权重的原则,如权重中所定义。通常,因此不可能配置 GridLayout 以在多行或多列之间以非平凡的比例分配多余的空间。尽管如此,一些常见的用例可能会被容纳如下。在单元组中的组件周围放置等量的空间;使用中心对齐(或重力)。完全控制行或列中多余的空间分布;使用 LinearLayout 子视图来保存关联单元组中的组件。使用这些技术中的任何一种时,请记住单元组可能会被定义为重叠。
For an example and some practical pointers, take a look at last year's blog post introducing the GridLayout
widget.
有关示例和一些实用提示,请查看去年介绍GridLayout
小部件的博客文章。
Edit:I don't think there's an xml-based approach to scaling the tiles like in the Google Play app to 'squares' or 'rectangles' twice the length of those squares. However, it is certainly possible if you build your layout programmatically. All you really need to know in order two accomplish that is the device's screen dimensions.
编辑:我不认为有一种基于 xml 的方法可以将 Google Play 应用程序中的瓷砖缩放为这些正方形长度两倍的“正方形”或“矩形”。但是,如果您以编程方式构建布局,这当然是可能的。为了实现两个目标,您真正需要知道的就是设备的屏幕尺寸。
Below a (very!) quick 'n dirty approximation of the tiled layout in the Google Play app.
在 Google Play 应用程序中平铺布局的(非常!)快速近似之下。
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
int halfScreenWidth = (int)(screenWidth *0.5);
int quarterScreenWidth = (int)(halfScreenWidth * 0.5);
Spec row1 = GridLayout.spec(0, 2);
Spec row2 = GridLayout.spec(2);
Spec row3 = GridLayout.spec(3);
Spec row4 = GridLayout.spec(4, 2);
Spec col0 = GridLayout.spec(0);
Spec col1 = GridLayout.spec(1);
Spec colspan2 = GridLayout.spec(0, 2);
GridLayout gridLayout = new GridLayout(this);
gridLayout.setColumnCount(2);
gridLayout.setRowCount(15);
TextView twoByTwo1 = new TextView(this);
GridLayout.LayoutParams first = new GridLayout.LayoutParams(row1, colspan2);
first.width = screenWidth;
first.height = quarterScreenWidth * 2;
twoByTwo1.setLayoutParams(first);
twoByTwo1.setGravity(Gravity.CENTER);
twoByTwo1.setBackgroundColor(Color.RED);
twoByTwo1.setText("TOP");
twoByTwo1.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByTwo1, first);
TextView twoByOne1 = new TextView(this);
GridLayout.LayoutParams second = new GridLayout.LayoutParams(row2, col0);
second.width = halfScreenWidth;
second.height = quarterScreenWidth;
twoByOne1.setLayoutParams(second);
twoByOne1.setBackgroundColor(Color.BLUE);
twoByOne1.setText("Staff Choices");
twoByOne1.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByOne1, second);
TextView twoByOne2 = new TextView(this);
GridLayout.LayoutParams third = new GridLayout.LayoutParams(row2, col1);
third.width = halfScreenWidth;
third.height = quarterScreenWidth;
twoByOne2.setLayoutParams(third);
twoByOne2.setBackgroundColor(Color.GREEN);
twoByOne2.setText("Games");
twoByOne2.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByOne2, third);
TextView twoByOne3 = new TextView(this);
GridLayout.LayoutParams fourth = new GridLayout.LayoutParams(row3, col0);
fourth.width = halfScreenWidth;
fourth.height = quarterScreenWidth;
twoByOne3.setLayoutParams(fourth);
twoByOne3.setBackgroundColor(Color.YELLOW);
twoByOne3.setText("Editor's Choices");
twoByOne3.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse);
gridLayout.addView(twoByOne3, fourth);
TextView twoByOne4 = new TextView(this);
GridLayout.LayoutParams fifth = new GridLayout.LayoutParams(row3, col1);
fifth.width = halfScreenWidth;
fifth.height = quarterScreenWidth;
twoByOne4.setLayoutParams(fifth);
twoByOne4.setBackgroundColor(Color.MAGENTA);
twoByOne4.setText("Something Else");
twoByOne4.setTextAppearance(this, android.R.style.TextAppearance_Large);
gridLayout.addView(twoByOne4, fifth);
TextView twoByTwo2 = new TextView(this);
GridLayout.LayoutParams sixth = new GridLayout.LayoutParams(row4, colspan2);
sixth.width = screenWidth;
sixth.height = quarterScreenWidth * 2;
twoByTwo2.setLayoutParams(sixth);
twoByTwo2.setGravity(Gravity.CENTER);
twoByTwo2.setBackgroundColor(Color.WHITE);
twoByTwo2.setText("BOTOM");
twoByTwo2.setTextAppearance(this, android.R.style.TextAppearance_Large_Inverse);
gridLayout.addView(twoByTwo2, sixth);
The result will look somewhat like this (on my Galaxy Nexus):
结果看起来有点像这样(在我的 Galaxy Nexus 上):
回答by lubosz
After many attempts I found what I was looking for in this layout. Even spaced LinearLayouts with automatically fitted ImageViews, with maintained aspect ratio. Works with landscape and portrait with any screen and image resolution.
经过多次尝试,我在这个布局中找到了我想要的东西。甚至间隔的 LinearLayouts 与自动拟合的 ImageViews,保持纵横比。适用于任何屏幕和图像分辨率的横向和纵向。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffcc5d00" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="10dip"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image1"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/stackoverflow"
android:layout_width="fill_parent"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="10dip"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image2"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/stackoverflow"
android:layout_width="fill_parent"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="10dip"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image3"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/stackoverflow"
android:layout_width="fill_parent"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:padding="10dip"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image4"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/stackoverflow"
android:layout_width="fill_parent"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
回答by Adnan Abdollah Zaki
Starting in API 21the notion of weight was added to GridLayout.
从API 21开始,权重的概念被添加到GridLayout 中。
To support older android devices, you can use the GridLayout from the v7 support library.
要支持较旧的 android 设备,您可以使用v7 支持库中的GridLayout。
compile 'com.android.support:gridlayout-v7:22.2.1'
The following XML gives an example of how you can use weights to fill the screen width.
以下 XML 给出了如何使用权重来填充屏幕宽度的示例。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:grid="http://schemas.android.com/apk/res-auto"
android:id="@+id/choice_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="4dp"
grid:alignmentMode="alignBounds"
grid:columnCount="2"
grid:rowOrderPreserved="false"
grid:useDefaultMargins="true">
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile1" />
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile2" />
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile3" />
<TextView
android:layout_width="0dp"
android:layout_height="100dp"
grid:layout_columnWeight="1"
grid:layout_gravity="fill_horizontal"
android:gravity="center"
android:background="#FF33B5E5"
android:text="Tile4" />
</android.support.v7.widget.GridLayout>
回答by Zed
If you use fragments you can prepare XML layout and than stratch critical elements programmatically
如果您使用片段,您可以准备 XML 布局,然后以编程方式对关键元素进行分层
int thirdScreenWidth = (int)(screenWidth *0.33);
View view = inflater.inflate(R.layout.fragment_second, null);
View _container = view.findViewById(R.id.rim1container);
_container.getLayoutParams().width = thirdScreenWidth * 2;
_container = view.findViewById(R.id.rim2container);
_container.getLayoutParams().width = screenWidth - thirdScreenWidth * 2;
_container = view.findViewById(R.id.rim3container);
_container.getLayoutParams().width = screenWidth - thirdScreenWidth * 2;
This layout for 3 equal columns. First element takes 2x2
Result in the picture
此布局为 3 个相等的列。第一个元素取图片中的 2x2 结果
回答by AllDayAmazing
Just a quick follow up and note that it is possible now to use the support library with weighted spacing in GridLayout to achieve what you want, see:
只需快速跟进并注意,现在可以在 GridLayout 中使用具有加权间距的支持库来实现您想要的,请参阅:
As of API 21, GridLayout's distribution of excess space accomodates the principle of weight. In the event that no weights are specified, the previous conventions are respected and columns and rows are taken as flexible if their views specify some form of alignment within their groups. The flexibility of a view is therefore influenced by its alignment which is, in turn, typically defined by setting the gravity property of the child's layout parameters. If either a weight or alignment were defined along a given axis then the component is taken as flexible in that direction. If no weight or alignment was set, the component is instead assumed to be inflexible.
从 API 21 开始,GridLayout 的多余空间分布符合权重原则。在未指定权重的情况下,如果列和行的视图在其组内指定了某种形式的对齐方式,则将遵守先前的约定,并且将列和行视为灵活。因此,视图的灵活性受其对齐方式的影响,而对齐方式通常通过设置子布局参数的重力属性来定义。如果沿给定轴定义了权重或对齐方式,则组件在该方向上被视为灵活。如果未设置权重或对齐方式,则假定组件是不灵活的。
回答by NaveenDA
I archive this using LinearLayout
我存档这个使用 LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:height="166dp"
android:text="Tile1"
android:gravity="center"
android:background="#6f19e5"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Tile2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:height="126dp"
android:gravity="center"
android:layout_weight=".50"
android:background="#f1d600"/>
<TextView
android:text="Tile3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:height="126dp"
android:gravity="center"
android:layout_weight=".50"
android:background="#e75548"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Tile4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:height="126dp"
android:gravity="center"
android:layout_weight=".50"
android:background="#29d217"/>
<TextView
android:text="Tile5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:height="126dp"
android:gravity="center"
android:layout_weight=".50"
android:background="#e519cb"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:height="176dp"
android:text="Tile6"
android:gravity="center"
android:background="#09eadd"/>
</LinearLayout>
</LinearLayout>
回答by Braden Holt
For other peeps: If you have to use GridLayout due to project requirements then use it but I would suggest trying out TableLayout as it seems much easier to work with and achieves a similar result.
对于其他窥视者:如果由于项目要求而必须使用 GridLayout,则使用它,但我建议尝试使用 TableLayout,因为它看起来更容易使用并获得类似的结果。
Docs: https://developer.android.com/reference/android/widget/TableLayout.html
文档:https: //developer.android.com/reference/android/widget/TableLayout.html
Example:
例子:
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<Button
android:id="@+id/test1"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:text="Test 1"
android:drawableTop="@mipmap/android_launcher"
/>
<Button
android:id="@+id/test2"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:text="Test 2"
android:drawableTop="@mipmap/android_launcher"
/>
<Button
android:id="@+id/test3"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:text="Test 3"
android:drawableTop="@mipmap/android_launcher"
/>
<Button
android:id="@+id/test4"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:text="Test 4"
android:drawableTop="@mipmap/android_launcher"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/test5"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:text="Test 5"
android:drawableTop="@mipmap/android_launcher"
/>
<Button
android:id="@+id/test6"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:text="Test 6"
android:drawableTop="@mipmap/android_launcher"
/>
</TableRow>
</TableLayout>
回答by Richard Kamere
<GridLayout
android:layout_width="match_parent"
android:layout_weight="3"
android:columnCount="2"
android:padding="10dp"
android:rowCount="3"
android:background="@drawable/background_down"
android:layout_height="0dp">
<androidx.cardview.widget.CardView
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="10dp"
android:elevation="10dp"
app:cardCornerRadius="15dp"
>
<LinearLayout
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_weight="2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="15dp"
android:src="@drawable/user" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Users"
android:textSize="16sp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="10dp"
android:elevation="10dp"
app:cardCornerRadius="15dp"
>
<LinearLayout
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_weight="2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="15dp"
android:src="@drawable/addusers" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Users"
android:textSize="16sp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="10dp"
android:elevation="10dp"
app:cardCornerRadius="15dp"
>
<LinearLayout
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_weight="2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="15dp"
android:src="@drawable/newspaper" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Report"
android:textSize="16sp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_margin="10dp"
android:elevation="10dp"
app:cardCornerRadius="5dp"
>
<LinearLayout
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:layout_weight="2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="15dp"
android:src="@drawable/settings" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:textSize="16sp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
Full tutorials can be found here .
可以在这里找到完整的教程。
[Android Grid Layout With CardView and OnItemClickListener][2]
回答by cox
Do you know View.getViewTreeObserver().addOnGlobalLayoutListener()
你知道 View.getViewTreeObserver().addOnGlobalLayoutListener()
By this you can calculate the sizes.
通过这个你可以计算尺寸。
I achieve your UI effect by GridView:
我通过 GridView 实现了你的 UI 效果:
GridView g;
g.setNumColumns(2);
g.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);