如何使自定义适配器填充gridview高度

时间:2013-05-06 12:01:15

标签: android android-layout android-gridview

我正在使用Sudoku app。布局是9x9 GridView。每个GridView都包含customAdapter的9个Textview。我想让9个TextViews填充每个GridView的高度。怎么样?

这就是我现在所拥有的enter image description here

这是myGridAdapter:

public class myGridAdapter extends BaseAdapter {

private Context context;
private String[] mobileValues;

public myGridAdapter(MainActivity mainActivity, String[] arrayEmpty) {
    this.context = mainActivity;
    this.mobileValues = arrayEmpty;
}

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 text_item.xml
        gridView = inflater.inflate(R.layout.text_item, null);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.grid_item_label);
        textView.setText(mobileValues[position]);

    } else {
        gridView = (View) convertView;
    }

    return gridView;
}
.....
}

这是每个文本项的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/grid_item_label"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/cell_shape"
    android:text="@string/_1"
    tools:ignore="InefficientWeight"
    android:textSize="12sp" >
</TextView>

</LinearLayout>

这是每个TextView的形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#777777" />
</shape>

我希望我的Gridview与此enter image description here

一样

应用bakriOnFire的第一个答案后

enter image description here

5 个答案:

答案 0 :(得分:5)

您可以将在GridView适配器中充气的TextView的每个LinearLayout的高度设置为screenHeight / 3.

在为textView的xml充气后的getView中,将膨胀的视图强制转换为LinearLayout并将其高度设置为:

LayoutInflater li = getLayoutInflater();
LinearLayout ll = (LinearLayout) li.inflate(R.layout.menu_items, null);

int screenHeight = ((Activity) context).getWindowManager()
                    .getDefaultDisplay().getHeight();

ll.setMinimumHeight(screenHeight/3);

修改

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent"
    android:layout_height="match_parent">

         

text_item.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/grid_item_label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    tools:ignore="InefficientWeight"
    android:padding="0dp"
    android:textSize="12sp" >
</TextView>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    String[] str = { "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
             "1", "2", "3", "1", "2", "3", "1", "2", "3" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        GridView gridView = (GridView) findViewById(R.id.gridView1);

        gridView.setAdapter(new myGridAdapter(this, str));

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

            }

        });

    }

    public class myGridAdapter extends BaseAdapter {

        private Context context;
        private String[] mobileValues;

        public myGridAdapter(MainActivity mainActivity, String[] arrayEmpty) {
            this.context = mainActivity;
            this.mobileValues = arrayEmpty;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            LinearLayout gridView;

            if (convertView == null) {

                // get layout from text_item.xml
                gridView = (LinearLayout)inflater.inflate(R.layout.text_item, null);

                int screenHeight = ((Activity) context).getWindowManager()
                        .getDefaultDisplay().getHeight();

                gridView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, screenHeight/9));


                //gridView.setMinimumHeight(screenHeight/9);
                // set value into textview
                TextView textView = (TextView) gridView
                        .findViewById(R.id.grid_item_label);
                textView.setText(mobileValues[position]); 

            } else {
                gridView = (LinearLayout) convertView;
            }

            return gridView;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mobileValues.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }
    }

}

在getView中我所做的是我已经将text_item.xml膨胀了9x9次,其中我已经将text_item的布局高度动态设置为screenHeight / 9。 如果它对您有用,请告诉我。

修改2

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            android:layout_width="match_parent"
        android:layout_height="match_parent">
<GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
        android:gravity="center"
        android:numColumns="9"
        android:padding="0dp"
        android:background="#000000"
        android:horizontalSpacing="3dp"
        android:verticalSpacing="3dp"
        android:stretchMode="columnWidth" >
    </GridView>
    </LinearLayout>

text_item.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FFFFFF"
android:orientation="vertical" >

<TextView
    android:id="@+id/grid_item_label"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    tools:ignore="InefficientWeight"

    android:padding="0dp"
    android:textSize="12sp" >
</TextView>

</LinearLayout>

答案 1 :(得分:2)

您可以获得设备高度宽度和3行n col除以3,并设置编程高度宽度可以解决您的问题。

答案 2 :(得分:1)

试用此代码

<GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"


        android:columnWidth="10dp"
        android:gravity="center"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:stretchMode="columnWidth"
        android:verticalSpacing="10dp" >
    </GridView>

这可能有助于你

答案 3 :(得分:1)

请看看这个数独的开源示例。

http://code.google.com/p/opensudoku-android/

这将帮助您绘制9X9网格视图。

嗨,这段代码。

<TextView   
        android:id="@+id/num"       
        android:text="1"
        android:gravity="center"
        android:textStyle="bold"        
        android:textColor="@android:color/darker_gray"
        android:textSize="25sp"     
        android:layout_centerInParent="true"
        android:layout_width="fill_parent"      
        android:layout_height="fill_parent">
    </TextView>

答案 4 :(得分:1)

由于GridView将在调用适配器时起作用,并且没有每个单元格/子项的高度属性,因此最佳选择可能是创建所需的w / h图像并将其设置为textView.SetBackGround。 ..

这是我用于我的Sudoku应用程序的内容。它在Play商店,请查看屏幕,让我知道你的想法。

在PlayStore - EasySudoku,免费下载 - http://tinyurl.com/c4qra79