【发布时间】:2014-08-26 08:45:55
【问题描述】:
我通过 GridView 绘制一个矩阵。在 4x4 的情况下,使用默认值可以正常工作(每行包含 4 个项目)。绘制 5x5 时会出现问题,每行只有 4 个项目。
我的想法:调整项目宽度 =(屏幕宽度)/每行项目数。没关系?如果它是正确的,有人可以给我代码来做到这一点(对不起,我是 Android 新手)
或者,谁能在所有情况下(5x5、6x6 等)给我其他想法或代码示例?
--- 我的代码 --- MainLayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:layout_margin="0dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center" >
</GridView>
图像适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//return 0;
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
//green code: BBE833
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell, R.drawable.green_cell,
R.drawable.green_cell
};
MyActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
SecureRandom securerandom = new SecureRandom();
int randomLoop = 15;
final GridView gridview = (GridView)findViewById(R.id.gridView1);
final ImageAdapter imageAdapter = new ImageAdapter(this);
for (int i = 0; i < randomLoop; i++) {
int position = securerandom.nextInt(25);
imageAdapter.updateCell(position);
}
gridview.setAdapter(imageAdapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(QuizActivity.this, "" + position, Toast.LENGTH_SHORT).show();
imageAdapter.updateCell(position);
//a = 11;
gridview.setAdapter(imageAdapter);
}
});
p/s:我只是硬编码 5x5 尺寸 谢谢。
【问题讨论】:
-
也粘贴您的布局代码。
-
@Haresh:你能教我如何使用双向gridview吗?谢谢
-
请从 github 下载 zip 并从中获取示例代码。
-
@Haresh:好的。但是,我阅读了描述,也许它不适用于这种情况
标签: android android-layout android-gridview