Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 可扩展的Gridview布局_Android_Android Layout_Android Gridview - Fatal编程技术网

Android 可扩展的Gridview布局

Android 可扩展的Gridview布局,android,android-layout,android-gridview,Android,Android Layout,Android Gridview,我为我侄子制作了一个简单的卡片匹配游戏,创建了一个缩放、等距的gridview布局。它的行为和我想要的差不多,但似乎应该有一种不太复杂的方法来实现这一点,所以我想知道是否有人可以建议一种更好/更简单的方法。我是Android的新手,所以很可能是我设计过度了 我有一个控制卡片数量(12-32)的设置,它可以扩展到任何大小的屏幕(各种大小和分辨率的平板电脑或手机) 以下是我的gridview XML: <GridView xmlns:android="http://schemas.androi

我为我侄子制作了一个简单的卡片匹配游戏,创建了一个缩放、等距的gridview布局。它的行为和我想要的差不多,但似乎应该有一种不太复杂的方法来实现这一点,所以我想知道是否有人可以建议一种更好/更简单的方法。我是Android的新手,所以很可能是我设计过度了

我有一个控制卡片数量(12-32)的设置,它可以扩展到任何大小的屏幕(各种大小和分辨率的平板电脑或手机)

以下是我的gridview XML:

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview"
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="@drawable/wood"
android:layout_below="@id/score_board"
android:layout_above="@+id/messageparent"
/>
以下是我的sizeCardsToFit方法中的相关代码:

//... algorithm to calc cols, width and height to fit screen

gridview.setNumColumns(nCols);
gridview.setColumnWidth(width);

for (int i = 0; i < gridview.getChildCount(); i++) {
    ((CardView)gridview.getChildAt(i)).setLayoutParams(new GridView.LayoutParams(width,height));
}

mCellHeight=height;
mCellWidth=width;
/。。。计算颜色、宽度和高度以适应屏幕的算法
gridview.setNumColumns(nCols);
gridview.setColumnWidth(宽度);
对于(int i=0;i

//... algorithm to calc cols, width and height to fit screen

gridview.setNumColumns(nCols);
gridview.setColumnWidth(width);

for (int i = 0; i < gridview.getChildCount(); i++) {
    ((CardView)gridview.getChildAt(i)).setLayoutParams(new GridView.LayoutParams(width,height));
}

mCellHeight=height;
mCellWidth=width;
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyGrid" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:padding="0dp"
    android:verticalSpacing="2dp" android:horizontalSpacing="2dp"
    android:scrollingCache="true" android:smoothScrollbar="true"
    android:clipChildren="true" android:alwaysDrawnWithCache="true"
    android:numColumns="auto_fit" android:columnWidth="100dp"
    android:stretchMode="columnWidth" android:gravity="center_horizontal"
    android:background="#000000">
</GridView>