我第一次使用GridView。一切都很好但图像高度较低。我搜索了很多,但无法通过XML获得增加图像高度的方法。这是我的代码:
activity_select_images.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbarThumbVertical="@drawable/custom_scroll_style"
android:fillViewport="false">
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectImagesActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_marginTop="8dp"
android:text="Please Select the pictures you like"
android:id="@+id/selectTextview"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_below="@+id/selectTextview"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="500dp"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
<Button
android:id="@+id/submit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Submit"
android:textSize="18dp"
android:textAllCaps="false"
android:textColor="#ffffff"
android:layout_below="@+id/frameLayout"
android:background="@drawable/my_button"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
</RelativeLayout>
</ScrollView>
gridview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/thumbImage"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="12dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<CheckBox android:id="@+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/thumbImage"
android:layout_alignLeft="@+id/thumbImage"
android:layout_alignStart="@+id/thumbImage"
android:layout_marginLeft="59dp"
android:layout_marginStart="59dp"
android:layout_marginBottom="61dp" />
</RelativeLayout>
SelectImagesActivity.java
package com.houssup.userapp;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class SelectImagesActivity extends AppCompatActivity {
private int count = 0;
Button btn;
private Drawable marker, marker1;
private boolean[] thumbnailsselection;
private String[] arrPath;
private ImageAdapter imageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_images);
GridView imagegrid = (GridView) findViewById(R.id.gridView);
btn = (Button) findViewById(R.id.submit);
marker = getResources().getDrawable(R.drawable.pic_five);
marker1 = getResources().getDrawable(R.drawable.pic_two);
imagegrid.setAdapter(new ImageAdapter(this, marker, marker1));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (count < 5)
Toast.makeText(SelectImagesActivity.this, "Please " +
"select total 5 images", Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private Drawable drawable1, drawable2;
private LayoutInflater mInflater;
int positionID[];
public ImageAdapter(Context context, Drawable drawable1,
Drawable drawable2) {
this.context = context;
this.drawable1 = drawable1;
this.drawable2 = drawable2;
}
public int getCount() {
return 6;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from grid_item.xml ( Defined Below )
gridView = inflater.inflate(R.layout.gridview_layout, null);
ImageView imageView = (ImageView) gridView.findViewById(R.id.thumbImage);
CheckBox checkBox = (CheckBox) gridView.findViewById(R.id.itemCheckBox);
if (position < 5) {
imageView.setImageDrawable(marker);
} else {
imageView.setImageDrawable(marker1);
}
// set image based on selected text
} else {
gridView = (View) convertView;
}
return gridView;
}
}
class ViewHolder {
ImageView imageview;
CheckBox checkbox;
int id;
}
}