GridView有4个android项目

时间:2013-01-20 13:37:39

标签: java android gridview uiimageview

我正在尝试制作网格视图,因为这对更多设备更好。我已经尝试在界面构建器中添加我的按钮(因为我将它们添加为ImageButtons,因为它的大小并不好),结果就是在这个屏幕中:http://i.imgur.com/pTJnKOv.png

任何人都可以通过在GridView中制作它来帮助我,完全相同但在所有设备上兼容。

提前致谢;)!

这是ImageAdapter:

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

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

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.mobile, null);


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

        String button = buttonValues[position];



        if (button.equals("homework")) {
            imageView.setImageResource(R.drawable.homework);
        } else if (button.equals("schedule")) {
            imageView.setImageResource(R.drawable.schedulebut);
        } else if (button.equals("planner")) {
            imageView.setImageResource(R.drawable.plannerbut);
        } else {
            imageView.setImageResource(R.drawable.settingsbut);
        }

    } else {
        gridView = (View) convertView;
    }

    return gridView;
}

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

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

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

}

这是我在onCreate中的方法:

static final String[] MOBILE_OS = new String[] { "Homework", "Schedule",
        "Planner", "Settings" };
private void setGridView() {
    gridView = (GridView) findViewById(R.id.gridView1);

    gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));

    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            Toast.makeText(getApplicationContext(), "LOL!", Toast.LENGTH_SHORT).show();

        }
    });

}

2 个答案:

答案 0 :(得分:1)

由于您尝试使用 2X2网格并且只有4个元素,因此请使用 android:numColumns="2" 作为GridView的属性。

此外,还有这个优秀的tutorial on GridViews

仅在网格中实现图片的代码段:

public class ImageAdapter extends BaseAdapter {
    private Context context;


    public ImageAdapter(Context context) {
        this.context = context;
    }

    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.mobile, null);


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



            if (mobile.equals("Windows")) {
                imageView.setImageResource(R.drawable.windows_logo);
            } else if (mobile.equals("iOS")) {
                imageView.setImageResource(R.drawable.ios_logo);
            } else if (mobile.equals("Blackberry")) {
                imageView.setImageResource(R.drawable.blackberry_logo);
            } else {
                imageView.setImageResource(R.drawable.android_logo);
            }

        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

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

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

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

}

编辑:您在所有按钮中获得相同的图像,因为您匹配在不同情况下编写的字符串。 (你在一个地方使用&#34;家庭作业&#34; ,在另一个地方使用&#34;家庭作业&#34;

所以,使用static final String[] MOBILE_OS = new String[] { "homework", "schedule", "planner", "settings" };,它会正常工作。

答案 1 :(得分:0)

要获得固定尺寸,您可以添加

机器人:layout_height = “”  机器人:layout_width = “”

示例:

机器人:layout_height = “100dp”  机器人:layout_width = “200dp”

按钮标签中的