在Android中创建gridview

时间:2014-04-11 20:30:44

标签: android gridview

我正在尝试创建一个像instagram

中的网格视图

使用以下代码。

public class MainActivity extends Activity  {

     static final String[] alphabets = new String[] { 
         "A", "B", "C", "D", "E",
            "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T",
            "U", "V", "W", "X"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, alphabets);

            // create a RelativeLayout
            RelativeLayout relativeLayout = new RelativeLayout(this);

            // define the RelativeLayout layout parameters.
            RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.FILL_PARENT);

            // create a gridview
            GridView gridView= new GridView(this);

            gridView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            gridView.setNumColumns(4);

            gridView.setMinimumHeight(300);
            gridView.setMinimumWidth(300);
            gridView.setColumnWidth(300);

            gridView.setAdapter(adapter);
            relativeLayout.setPadding(50, 50, 50, 50);
            // Adding the gridview to the RelativeLayout as a child
            relativeLayout.addView(gridView);

            // set the RelativeLayout as our content view
            setContentView(relativeLayout, relativeLayoutParams);



    }

结果就像这样image 我试过放高度和宽度,但仍然无法正常工作。为什么?。看起来像一个矩形。 还有什么想法如何设置边框?我需要实现这样的目标

image1,边框为黑色,宽度和列相同,每个元素都与Instagram中的元素相同。样本和教程真的很有帮助。

1 个答案:

答案 0 :(得分:0)

您需要添加自定义ArrayAdapter和自定义网格项

在MainActivity类中实现自定义ArrayAdapter

public static class CustomArrayAdapter extends ArrayAdapter<String> {

        private List<String> strings;

        /**
         * 
         * @param context
         * @param resource
         * @param objects
         *            your strings to show
         */
        public CustomArrayAdapter(Context context, int resource,
                List<java.lang.String> objects) {
            super(context, resource, objects);
            // TODO Auto-generated constructor stub
        }

        /**
         * Another constructor that takes Array instead of List
         * 
         * @param context
         * @param resource
         * @param objects
         *            your strings to show
         */
        public CustomArrayAdapter(Context context, int resource,
                String[] objects) {
            super(context, resource, objects);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View gridItem = inflater.inflate(R.layout.grid_view_item, parent, false);
            TextView tv = (TextView) gridItem.findViewById(R.id.item);
            tv.setText((CharSequence) strings.get(position));

            return gridItem;

        }

    }

并在res / layout中添加名为&#34; grid_view_item.xml&#34;

的自定义网格项布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

      <TextView
        android:id="@+id/item"
        android:textColor="#FFDDFF99"
        android:layout_width="200dp" 
        android:layout_height="200dp"
        android:layout_margin="5dp"
        android:text="Item" />


</LinearLayout>

您可以根据需要更改宽度和高度属性。使用margin属性将项目彼此分开