Creating gridview with 3 rows of imageviews that f

2019-02-20 13:41发布

问题:

I have been searching everywhere for similar solutions but none seem to work for me.

On my first screen I will have a gridview which consists of 1 column and 3 rows vertically. Each row will have an imageview and a partially transparent textview on top of the text view. The imageviews span the screen width fine. My only problem is that the 3 image views do not span the full screen vertically, there is space inbetween rows, although I have tried many methods to fix this. I will post up my xml files and code:

row_layout.xml - this is the gridview items

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
    android:id="@+id/item_image"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:layout_width="fill_parent"
    android:layout_height="180dp"
    android:src="@drawable/season1">
</ImageView>

<TextView
    android:id="@+id/item_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/item_image"
    android:layout_alignTop="@+id/item_image"
    android:layout_alignRight="@+id/item_image"
    android:layout_alignBottom="@+id/item_image"
    android:gravity="bottom|left"
    android:textSize="30sp"
    android:textAlignment="center"
    android:lines="1"
    android:layout_marginTop="145dp"
    android:background="#99000000">


</TextView>

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="0dp"
    android:padding="0dp"
    android:gravity="center"
    android:numColumns="1"
    android:fitsSystemWindows="true"
    android:stretchMode="columnWidth" >
</GridView>

My custom adapter:

public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();

public CustomGridViewAdapter(Context context, int layoutResourceId, ArrayList<Item> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RecordHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        row.setMinimumHeight(MainActivity.height1/3);

        holder = new RecordHolder();
        holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
        holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
        row.setTag(holder);
    } else {
        holder = (RecordHolder) row.getTag();
    }

    Item item = data.get(position);
    holder.txtTitle.setText(item.getTitle());
    holder.imageItem.setImageBitmap(item.getImage());// my image
    return row;

}

static class RecordHolder {
    TextView txtTitle;
    ImageView imageItem;

}

}

MainActivity:

public class MainActivity extends ActionBarActivity {

GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
public static int height1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    ActionBar ab = getActionBar();
    ab.setDisplayShowTitleEnabled(false);
    ab.setDisplayShowHomeEnabled(false);

    Resources res = this.getResources();

    Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.actionbar);
    BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
    ab.setBackgroundDrawable(actionBarBackground);

    //set grid view item

    Bitmap season1Icon = BitmapFactory.decodeResource(res, R.drawable.season1);
    Bitmap season2Icon = BitmapFactory.decodeResource(res, R.drawable.season2);
    Bitmap season3Icon = BitmapFactory.decodeResource(res, R.drawable.season3);


    gridArray.add(new Item(season1Icon,"Season 1"));
    gridArray.add(new Item(season2Icon,"Season 2"));
    gridArray.add(new Item(season3Icon,"Season 3"));

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    height1 = metrics.heightPixels;

    gridView = (GridView) findViewById(R.id.gridView1);
    customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);

    gridView.setAdapter(customGridAdapter);

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            Intent a = new Intent(MainActivity.this, House.class);
            a.putExtra("Position", position);
            startActivity(a);
        }

    });
}

}

Any help will be greatly appreciated, I've spent hours trying to fix this!

回答1:

Dynamically set the layout params of your imageview, once you know the height of the screen.

if (row == null) {
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    row = inflater.inflate(layoutResourceId, parent, false);
    //row.setMinimumHeight(MainActivity.height1/3);  //don't need this, since wrap content will make the row height match your image view's

    holder = new RecordHolder();
    holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
    holder.imageItem = (ImageView) row.findViewById(R.id.item_image);

    holder.imageItem.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, MainActivity.height1/3);
    row.setTag(holder);
} else {
    holder = (RecordHolder) row.getTag();
}