如何管理的GridViewGridView

2023-09-06 14:49:02 作者:荭妝

我有以下$ C $下的GridView(为了使日历)。

I have following code for Gridview (To make the Calendar).

    <GridView
    android:id="@+id/calendar"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/rl1"
    android:layout_below="@+id/ll2"
    android:columnWidth="250dp"
    android:numColumns="7" >
    </GridView>

Grid_Cell如下。

Grid_Cell is as follows.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/calendar_button_selector" >

<Button
    android:id="@+id/calendar_day_gridcell"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/calendar_button_selector"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#000"
    android:textSize="14.47sp" >
</Button>

<TextView
    android:id="@+id/num_events_per_day"
    style="@style/calendar_event_style"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >
</TextView>

我想设置的行和列之间没有空格。

I want to set no space between the rows and columns.

现有的观点是如下。

请帮我...在此先感谢

Please help me out... Thanks in Advance .

推荐答案

我会尽量给你一个片段,但说实话,没有一点使用一个GridView为你的情况,因为所有你项目有上横竖屏。您可以在一个小的循环,将得到的结果创建几个的LinearLayout 取值。

I will try to give you a snippet, but to be honest, there is no point of using a GridView for your case since all you items are there on the screen anyway. You can create a couple of LinearLayouts in a small loop that will get the result.

我会建议您根据屏幕的宽度上运行时设置columnWidth时。

I would advice you to set the columnWidth on Runtime according to the screen width.

和您的适配器应该与列的宽度和高度来喂儿童充气视图时进行设置。在这种情况下,你需要摆脱为numColumns的。请记住,使用为numColumns随着columnWidth中是没有意义的尤其是当你想要填满了整个空间。如果你想设置为numColumns,取出columnWidth中。

And your adapter should be fed with the column width and height to set them when inflating child views. And in this case, you need to get rid of numColumns. Remember that using numColumns along with columnWidth makes no sense especially when you want to fill the whole space. If you want to set the numColumns, remove the columnWidth.

解决方案:

下面是结果: 首先,我们创建的布局。就我而言,这是 MainActivity 的布局,被称为 activity_main.xml 。 (注意没有GridView的,因为我会在code表示在以后添加):

Here is the outcome: First, we create our layout. In my case, it is the MainActivity's layout and is called activity_main.xml. (Notice there is no GridView because I'll add that later in code):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/header"
        android:background="#444"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/white"
        android:text="Dynamic Static GridView" />

    <TextView
        android:id="@+id/footer"
        android:gravity="center"
        android:background="#444"
        android:textColor="@android:color/white"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Shush" />

</RelativeLayout>

我们的GridView控件元素的布局是在这里的 item_grid.xml

Our GridView element's layout is here in the item_grid.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#fff"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

现在的伎俩是MainActivity(我已经评论了一些code为你懂的):

Now the trick is in MainActivity (I have commented some of the code for you to understand):

包com.example.dynamicstaticgridview;

package com.example.dynamicstaticgridview;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.app.Activity;
import android.graphics.Color;

/**
 * @author Sherif elKhatib
 *
 */
public class MainActivity extends Activity {

    private static String items[]; //these are temporary items :p do not use them
    static {
        items = new String[7*7];
        for(int i=0;i<7*7;i++) {
            items[i] = String.valueOf(i);
        }
    }

    int numberOfColumns = 7; //defaulting to 7, you can change it when you know
    int numberOfRows = 7; //defaulting to 7, you can change it when you know
    GridView mGrid;
    ArrayAdapter<String> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.BELOW, R.id.header);
        params.addRule(RelativeLayout.ABOVE, R.id.footer);
        mGrid = new GridView(this) {
            @Override
            protected void onLayout(boolean changed, int l, int t, int r, int b) {
                super.onLayout(changed, l, t, r, b);
                if(!calculated)
                    getDimens();
            }
        };
        mGrid.setVerticalSpacing(0);
        mGrid.setHorizontalSpacing(0);
        mGrid.setStretchMode(GridView.NO_STRETCH);
        mGrid.setBackgroundColor(Color.rgb(100, 10, 10));
        ((RelativeLayout)findViewById(R.id.rootview)).addView(mGrid, params);
    }

    private int mCellWidth;
    private int mCellHeight;
    boolean calculated = false;
    protected void getDimens() {
        calculated = true;
        //here you might have some rounding errors
        //thats why you see some padding around the GridView
        mCellWidth = mGrid.getWidth()/numberOfColumns;
        mCellHeight = mGrid.getHeight()/numberOfRows;
        mGrid.setColumnWidth(mCellWidth);
        mGrid.setNumColumns(numberOfColumns);
        mAdapter = new ArrayAdapter<String>(this, R.layout.item_grid, R.id.text, items) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                GridView.LayoutParams params = null;
                if(convertView == null) {
                    params = new GridView.LayoutParams(mCellWidth, mCellHeight);
                }
                convertView = super.getView(position, convertView, parent);
                if(params != null) {
                    convertView.setLayoutParams(params);
                }
                return convertView;
            }
        };
        mGrid.setAdapter(mAdapter);
    }
}

注释: 你真不如找一个体面的算法来选择 mCellWidth mCellHeight numberOfColumns numberOfRows

COMMENTS: You'd really better find a decent algorithm to choose mCellWidth, mCellHeight, numberOfColumns, and numberOfRows.

 
精彩推荐
图片推荐