动态GridView for Android应用程序

时间:2016-01-04 04:35:53

标签: android gridview

我想生成一个动态网格视图,最少有2个按钮,最多16个按钮。按钮是图像,我希望图像根据网格视图中的按钮数量调整其大小。我没有使用任何库,因为我试图使用不同的库,但它们不起作用。我已经实现了自己的逻辑来生成具有可变按钮大小的动态网格视图。我面临的问题是它看起来并不专业,因为图像拉伸并且看起来很奇怪。这是我的动态网格视图活动的屏幕截图。我希望按钮的图像根据按钮的大小自动调整,即不被拉伸。

enter image description here

这是我的代码

Gridview.java

autoPosition

Gridview.xml

public void CreateGrid(int NoOfRooms, String names[]) {
    int RoomsOnTheRight = NoOfRooms / 2;
    int RoomsOnTheLeft = NoOfRooms - RoomsOnTheRight;
    Button[] Rooms = new Button[NoOfRooms];
    // create buttons and set their layout parameters
    for (int i = 0; i < NoOfRooms; i++) {
        float size = getSizeOfRoom(names[i]);
        Rooms[i] = new Button(this);
        @SuppressWarnings("deprecation")
        LinearLayout.LayoutParams lay = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, 0, size);
        lay.leftMargin = 4;
        lay.rightMargin = 4;
        lay.bottomMargin = 4;
        lay.topMargin = 4;
        Rooms[i].setLayoutParams(lay);
    }
    // Insert buttons on the right column of the GridView
    for (int i = 0; i < RoomsOnTheRight; i++) {
        LeftColumn.addView(Rooms[i]);
        Rooms[i].setText(NamesOfRooms[i]);
        Rooms[i].setGravity(Gravity.BOTTOM);
        Rooms[i].setTextColor(Color.parseColor("#ffffff"));
        AcquireImage(NamesOfRooms[i], Rooms[i]);
        Rooms[i].setTag("room" + (i + 1) + ".txt");
        Rooms[i].setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                OscarVoice.setSpeechRate(0.8f);
                OscarVoice.speak("Hello", TextToSpeech.QUEUE_FLUSH, null);
                Intent roomintent = new Intent("com.log.oscar.ROOMVIEW");
                roomintent.putExtra("filename", v.getTag().toString());
                startActivity(roomintent);
            }
        });

    }
    // Insert buttons on the left column of the GridView
    for (int i = 0; i < RoomsOnTheLeft; i++) {
        RightColumn.addView(Rooms[i + RoomsOnTheRight]);
        Rooms[i + RoomsOnTheRight]
                .setText(NamesOfRooms[i + RoomsOnTheRight]);
        Rooms[i + RoomsOnTheRight].setGravity(Gravity.BOTTOM);
        Rooms[i + RoomsOnTheRight]
                .setTextColor(Color.parseColor("#ffffff"));
        AcquireImage(NamesOfRooms[i + RoomsOnTheRight], Rooms[i
                + RoomsOnTheRight]);
        Rooms[i + RoomsOnTheRight].setTag("room"
                + (i + RoomsOnTheRight + 1) + ".txt");
        Rooms[i + RoomsOnTheRight]
                .setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent roomintent = new Intent(
                                "com.log.oscar.ROOMVIEW");
                        roomintent.putExtra("filename", v.getTag()
                                .toString());
                        startActivity(roomintent);
                    }
                });

    }

}

0 个答案:

没有答案