15

如何创建一个自定义布局,它将成为gridview的一个项目。我找不到任何教程之类的。

4

1 回答 1

26

尝试这个..

grid_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" > 

</GridView>

AndroidGridLayoutActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class AndroidGridLayoutActivity extends Activity {

        // Keep all Images in array
        public Integer[] mThumbIds = {
                 R.drawable.ic_launcher, R.drawable.ic_launcher,
                 R.drawable.ic_launcher, R.drawable.ic_launcher,
                 R.drawable.ic_launcher, R.drawable.ic_launcher,
                 R.drawable.ic_launcher, R.drawable.ic_launcher,
                 R.drawable.ic_launcher, R.drawable.ic_launcher,
                 R.drawable.ic_launcher, R.drawable.ic_launcher,
                 R.drawable.ic_launcher, R.drawable.ic_launcher,
                 R.drawable.ic_launcher
               };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);

        GridView gridView = (GridView) findViewById(R.id.grid_view);

        // Instance of ImageAdapter Class
        gridView.setAdapter(new ImageAdapter(this,mThumbIds));
    }
}

项目.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="50px"
        android:layout_height="50px"
        android:layout_marginRight="10px">
    </ImageView>

</LinearLayout>

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private final String[] mThumbIds;

    public ImageAdapter(Context context, String[] mThumbIds) {
        this.context = context;
        this.mThumbIds = mThumbIds;
    }

    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 mobile.xml
            gridView = inflater.inflate(R.layout.item, null);

            // set image based on selected text
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);


            imageView.setImageResource(mThumbIds[position]);


        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

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

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

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

}

如果您需要更多示例,请参阅以下链接。

http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76&aaid=100

http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html

http://learnandroideasily.blogspot.in/2013/09/android-custom-gridview-example.html

于 2013-10-31T06:16:04.400 回答