我使用以下代码更改子网格布局中的边距:
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);
我收到错误消息
android.widget.GridLayout $ LayoutParams无法强制转换为android.widget.LinearLayout $ LayoutParams
我的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>
我应该更改什么才能摆脱错误?
答案 0 :(得分:1)
您无法将LinearLayout.LayoutParams指定给GridLayout。有关如何以编程方式调整GridLayout参数的示例,请参阅this SO answer。