如何使customAdapter填充gridview的高度(How to make customAd

2019-09-02 02:02发布

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

这是我现在有什么

这里是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是这样一个

应用bakriOnFire的第一个答案后

Answer 1:

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

在getView充气的TextView的XML后,铸充气视图的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什么,我所做的是我已经膨胀为9×9倍text_item.xml,其中我已经动态设置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>


Answer 2:

你可以得到设备的高度和宽度由3两3行n山坳鸿沟,并设置programmtically高度宽度可以解决你的问题。



Answer 3:

试试这个代码

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

这可能会帮助ü



Answer 4:

请看看为soduku这个开源的样本。

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

这将帮助你画出9 X9 grdiview。

你好这个代码。

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


Answer 5:

由于GridView控件将功能时,您的适配器被调用,并没有对每个小区/子height属性,你最好的选择可能是创建所需的W / H的图像,并将其设置为textView.SetBackGround ...

这是我用我的数独的应用程序。 正是在Play商店,请查看屏幕,让我知道你的想法。

在Play商店中- EasySudoku,免费下载- http://tinyurl.com/c4qra79



文章来源: How to make customAdapter fill gridview height