GridView项目太小 - Android

时间:2015-02-14 02:48:45

标签: android gridview baseadapter

我关注this tutorial,它非常简单,但我的GridView图像比教程中的图像要小得多。有什么突出的错误代码吗?下面我已经包含了我的主要活动,我的适配器类以及带有xml的{​​{1}}。

应该是这样的:

enter image description here

相反,我的是这样的:

enter image description here

SitesActivity.java

GridView

GridViewSitesAdapter.java

package org.azurespot.cutelinks;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;

import org.azurespot.R;

public class SitesActivity extends ActionBarActivity {

    private GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sites);

        // with fragments, make sure you include the rootView when finding id
        gridView = (GridView) findViewById(R.id.sites_grid);
        // Set the Adapter to GridView
        gridView.setAdapter(new GridViewSitesAdapter(this));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sites, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_sites.xml

package org.azurespot.cutelinks;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import org.azurespot.R;

/**
 * Created by mizu on 2/11/15.
 */
public class GridViewSitesAdapter extends BaseAdapter {

    public Context mContext;

    public GridViewSitesAdapter(Context c) {
        mContext = c;
    }

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.cute_overload, R.drawable.attack_of_the_cute,
            R.drawable.zoo_borns, R.drawable.cutest_paw,
            R.drawable.mochimochiland, R.drawable.baby_mugging,
            R.drawable.cutest_food, R.drawable.tiny_cute_things,
            R.drawable.etsy_robot_plush
    };

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
        return imageView;

    }
}

1 个答案:

答案 0 :(得分:1)

为什么不根据屏幕宽度计算gridview项目的宽度

columnWidth = (int) ((getScreenWidth() - ((AppConstant.NUM_OF_COLUMNS + 1) * padding)) / AppConstant.NUM_OF_COLUMNS);

希望这段代码可以帮助您理解。如果您需要任何帮助,请告诉我。

谢谢:)