如何使customAdapter填充gridview的高度 [英] How to make customAdapter fill gridview height

查看:225
本文介绍了如何使customAdapter填充gridview的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在数独的应用程序。布局是9x9的GridView控件。每个GridView控件包括9 Textviews由customAdapter。我想使9 TextViews填写每个GridView控件的高度。怎么样?

I am working on Sudoku app. The layout is 9x9 GridView. Each GridView include 9 Textviews by customAdapter. I want to make the 9 TextViews to fill the height of each GridView. How?

这是我现在有什么

This is what I am having now

下面是myGridAdapter:

Here is 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的

This is the shape for each 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是像这样的

I want my Gridview to be like this one

应用bakriOnFire的第一个答案后

After applying the first answer of bakriOnFire

推荐答案

您可以设置TextView中的每个的LinearLayout,你在你的GridView的适配器screenHeight / 3膨胀的高度。

you can set the height of each LinearLayout of TextView that you inflate in your gridView's adapter to screenHeight/3.

在getView充气的TextView的XML后,投虚增视图的LinearLayout并设置其高度: -

In getView after inflating the textView's xml, cast the inflated view to LinearLayout and set its height as:-

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

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

ll.setMinimumHeight(screenHeight/3);

我检查了这一点,其working..hope这有助于ü..

i've checked this and its working..hope this helps u..

修改

activity_main.xml中

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

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

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什么,我所做的是我已经为充气9x9的次text_item.xml,其中我已经动态设置text_item的布局高度screenHeight / 9。
让我知道,如果它为你工作或没有。

In the getView what i've done is i've inflated the text_item.xml for 9x9 times, in which i've dynamically set the layout height of text_item to screenHeight/9. Let me know if it work for you or not.

编辑2

activity_main.xml中

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

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>

这篇关于如何使customAdapter填充gridview的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆