我通过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>
ImageAdapter
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 感谢。
答案 0 :(得分:0)
要获取GridView
中的列数,请使用方法getNumColumns()
例如:
gridview.getNumColumns();
http://developer.android.com/reference/android/widget/GridView.html#getNumColumns()
答案 1 :(得分:0)
要获取GridLayout中的项目数,请使用getChildCount()
方法