[英]Android: How to set up Grid Layout inside Linear layout?
I am using the following code to change the margins in the child grid layout: 我正在使用以下代码来更改子网格布局中的边距:
GridLayout grid = (GridLayout) findViewById(R.id.grid);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.setMargins(15,marginTop,15,marginBottom);
grid.setLayoutParams(params);
I am getting error message 我收到错误消息
android.widget.GridLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
android.widget.GridLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams
My XML file is: 我的XML文件是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:background="#fff5f5f5"
tools:context=".LinearLayout" >
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:layout_marginBottom="100dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center"
android:columnCount="8"
android:rowCount="8"
android:orientation="horizontal">
</GridLayout>
</LinearLayout>
What should I change to get rid of the error? 我应该怎么做才能消除错误?
You cannot assign LinearLayout.LayoutParams to GridLayout. 您不能将LinearLayout.LayoutParams分配给GridLayout。 Please see this SO answer for an example on how to programmatically adjust GridLayout parameters.
请参阅此SO答案 ,以获取有关如何以编程方式调整GridLayout参数的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.