简体   繁体   中英

How to create grids using Total rows and columns in android dynamically

I am new to android. I want to build a grid based on total rows and columns and display in view.After creating a grid I need to place icon in one of the grid. The icon will come from the server URL. The grid should be like this below. 在此处输入图片说明

That created grid should fit in the screen. How do I do that? Can someone give me a code sample. Thanks in advance!!!

res/layout/ gridview_android_example_with_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <GridView
        android:id="@+id/gridview_android_example"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:columnWidth="100dp"
        android:gravity="center"
        android:minHeight="90dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth" />

</LinearLayout>

AndroidGridViewDisplayImages.java

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class AndroidGridViewDisplayImages extends AppCompatActivity {

    GridView androidGridView;


// Dummy Array of images (Replace with your own values)
    Integer[] imageIDs = {
            R.drawable.email, R.drawable.mobile, R.drawable.alram,
            R.drawable.android, R.drawable.wordpress, R.drawable.web,
            R.drawable.email, R.drawable.mobile, R.drawable.alram,
            R.drawable.android, R.drawable.wordpress, R.drawable.web,
            R.drawable.email, R.drawable.mobile, R.drawable.alram,
            R.drawable.android, R.drawable.wordpress, R.drawable.web,
    };

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

        androidGridView = (GridView) findViewById(R.id.gridview_android_example);
        androidGridView.setAdapter(new ImageAdapterGridView(this));

        androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent,
                                    View v, int position, long id) {
                Toast.makeText(getBaseContext(), "Grid Item " + (position + 1) + " Selected", Toast.LENGTH_LONG).show();
            }
        });

    }

    public class ImageAdapterGridView extends BaseAdapter {
        private Context mContext;

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

        public int getCount() {
            return imageIDs.length;
        }

        public Object getItem(int position) {
            return null;
        }

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView mImageView;

            if (convertView == null) {
                mImageView = new ImageView(mContext);
                mImageView.setLayoutParams(new GridView.LayoutParams(130, 130));
                mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                mImageView.setPadding(16, 16, 16, 16);
            } else {
                mImageView = (ImageView) convertView;
            }
            mImageView.setImageResource(imageIDs[position]);
            return mImageView;
        }
    }
}

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