我在安装GridView时遇到了Android间距问题。基本上,我正在组装一个9x9游戏板。我有一个GridView正常工作,但是在水平方向上每个定义正方形后都有不必要的间距(垂直正常) - 因此它不能正确适合工作区域。
编辑:看起来GridView已经为每平方宽度分配了50dp,当我只想要它是30dp时...不确定为什么或如何更改它?自定义网格代码:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class CustomGrid extends BaseAdapter {
private Context mContext;
private final int[] Imageid;
public CustomGrid(Context c, int[] Imageid) {
mContext = c;
this.Imageid = Imageid;
}
@Override
public int getCount() {
return Imageid.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.gridsingle, null);
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
imageView.setPadding(0,0,0,0);
imageView.setAdjustViewBounds(true);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
主要观点摘录:
public class Gameplay extends Activity {
GridView grid;
int[] imageId = {
R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,
R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square, R.drawable.square,R.drawable.square
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gameplay);
CustomGrid adapter = new CustomGrid(Gameplay.this, imageId);
grid=(GridView)findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Gameplay.this, "You Clicked x", Toast.LENGTH_SHORT).show();
}
});
gridview 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"
tools:context=".MainActivity" >
<GridView
android:numColumns="9"
android:gravity="center"
android:columnWidth="30dp"
android:stretchMode="none"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="0dp"
android:id="@+id/grid"
/>
</LinearLayout>
imageview xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="30dp"
android:layout_height="30dp"
android:padding="0dp" >
<ImageView
android:id="@+id/grid_image"
android:layout_width="30dp"
android:layout_height="30dp"
android:stretchMode="none">
</ImageView>
</LinearLayout>
最终结果如下:
我尝试过调整宽度和使用StretchMode参数等明显的想法,但还没有运气。有什么想法吗?
非常感谢。
答案 0 :(得分:1)
我解决了我的问题 - 事实证明我的gridview(id网格)有相互冲突的布局 - 一旦我删除了废弃的布局,一切都开始工作了!