简体   繁体   中英

Gridlayout with 2x2 imageview android

I,ve got a gridview that gets 4 imageviews from an adapter, i want to have them 2x2 resized and fitted on my screen.

here´s the code for that:

@Override
protected View createQuizContentView() {
    mAnswerView = new GridView(getContext());

    StateListDrawable selector = new StateListDrawable();
    selector.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(Color.LTGRAY));
    selector.addState(new int[]{-android.R.attr.state_pressed}, new ColorDrawable(Color.WHITE));

    mAnswerView.setSelector(selector);
    //mAnswerView.setSelector(R.drawable.selector_button);

    mAnswerView.setNumColumns(2);
    mAnswerView.setAdapter(new OptionsQuizAdapter(getQuiz().getOptions(),
            R.layout.imgitem_answer));


    mAnswerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mAnswered = position;
            allowAnswers(mAnswerView,position);
        }
    });
    return mAnswerView;
}

and

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        convertView = inflater.inflate(mLayoutId, parent, false);
    }
    final ImageView imageView;
    String text = getText(position);
    Context context = parent.getContext();

    Resources resources = context.getResources();

    String stripped = text.replace("-", "");
    final int resourceId = resources.getIdentifier(stripped, "drawable",
            context.getPackageName());

    Drawable d = ContextCompat.getDrawable(context, resourceId);

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int height = size.y;

    double dviewheight = height/3.32;
    int viewheight = (int)dviewheight;

    imageView = (ImageView) convertView;
    imageView.setPadding(64, 64, 64, 64);

    convertView.setLayoutParams(newGridView.LayoutParams(GridView.AUTO_FIT, viewheight));

    imageView.setImageDrawable(d);

    return imageView;
}

At the moment i´am setting the height of the imageview inside the gridview by getting the screensize and dividing it which works fine on most device resolution but i still would prefer a more dynamic solution that would work if i added more rows to the gridview.

This is how it looks:

Screensize/magic number

When i use GridView.AUTO_FIT on the height it will add a lot of useless space and i get a scrollbar on the right like this:

Useless space vertically

I have tried several configurations and stretches but none seems to work. Any way to achieve what i want programmatically without xml?

试试这个:gridView.setNumColumns(2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM