0

我正在尝试制作网格视图,因为这对更多设备来说更好。我已经尝试在界面构建器中添加我的按钮(作为 ImageViews,因为我已将它们添加为 ImageButtons,但尺寸不好),结果出现在此屏幕中:http: //i.imgur.com/pTJnKOv .png

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

提前致谢 ;)!

这是图像适配器:

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();

        }
    });

}
4

2 回答 2

1

Since you are trying to use a 2X2 Grid and have only 4 elements, use android:numColumns="2" as an attribute of your GridView.

Additionally, there is this excellent tutorial on GridViews!

Code snippet to implement only Images in your Grid :

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;
    }

}

EDIT : You are getting the same image in all the buttons because you are matching the strings which you have written in different cases. (You are using "Homework" in one place and "homework" in another)

So, use static final String[] MOBILE_OS = new String[] { "homework", "schedule", "planner", "settings" }; and it would work fine.

于 2013-01-20T13:45:05.247 回答
0

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

android:layout_height="" android:layout_width=""

例子 :

android:layout_height="100dp" android:layout_width="200dp"

在您的按钮标签中

于 2013-01-20T14:17:25.173 回答