[英]Android: How to set up Grid Layout inside Linear 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);
我收到錯誤消息
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>
我應該怎么做才能消除錯誤?
您不能將LinearLayout.LayoutParams分配給GridLayout。 請參閱此SO答案 ,以獲取有關如何以編程方式調整GridLayout參數的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.