我创建了一个网格视图,我在其中显示图像。我试图从应用程序:2048重新创建外观。但是,我遇到了它看起来的几个问题。图像显示为3x3网格,这是正确的,但是,由于某种原因,列之间存在大量间隙。这是我的GridView XML:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:verticalSpacing="10dp" >
</GridView>
这是ImageAdapter:
package view;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import big.sound.soundtapper.R;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = { // 9 Tiles
R.drawable.bev_tile_default, R.drawable.bev_tile_default,
R.drawable.bev_tile_default, R.drawable.bev_tile_default,
R.drawable.bev_tile_default, R.drawable.bev_tile_default,
R.drawable.bev_tile_default, R.drawable.bev_tile_default,
R.drawable.bev_tile_default };
// Constructor
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(120, 120));
return imageView;
}
}
我正在尝试将表格置于页面中心,并摆脱列之间的大空间。
截图:
答案 0 :(得分:2)
试试这个: -
<GridView android:id="@+id/gridView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="4dp" android:columnWidth="80dp" android:gravity="center" android:stretchMode="columnWidth" > </GridView>
答案 1 :(得分:2)
它对我有用......
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/&GT;
答案 2 :(得分:2)
试试这个:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:horizontalSpacing="0dp"
android:verticalSpacing="0dp"
android:adjustViewBounds="true"
android:numColumns="3" >
希望它会对你有所帮助。
答案 3 :(得分:1)
尝试自动调整,并使用verticalspacing和stretchmode,如下所示。 AutoFit最好填充屏幕然后你可以播放是否有3x3。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFCC00" >
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="150dp"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform" >
</GridView>
</LinearLayout>