Android GridView使项目适合每个屏幕尺寸

MAX*_*GEN 3 android gridview android-layout

我正在使用gridvew作为布局来填充我的项目,圆形按钮,但是我在安装大网格时遇到了问题,11 x 11.当我使用更大的屏幕时,它适合但它会停止一半?这是因为我把我的roundbutton.xml中的szie?在我的手机较小的屏幕上,它确实适合并延伸到屏幕末端的末端.我需要适合屏幕,无论大小为11 x 11?

gridview布局

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridView1"
    android:numColumns="11"
    android:gravity="center"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</GridView>
Run Code Online (Sandbox Code Playgroud)

item.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="O"
android:id="@+id/grid_item_label"
android:layout_column="1" 
android:background = "@drawable/roundedbutton"/>
Run Code Online (Sandbox Code Playgroud)

roundedbutton.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#9F2200"/>
    <stroke android:width="2sp" android:color="#fff" />
        <size 
       android:width="5dp"
        android:height="5dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

activity_main.java

gridView = (GridView) findViewById(R.id.gridView1);
        customGridAdapter = new CustomGridViewAdapter(this, R.layout.button_item, gridArray);
        gridView.setAdapter(customGridAdapter);
Run Code Online (Sandbox Code Playgroud)

adapter.java

@Override
public View getView(final int position, View convertView, final ViewGroup parent)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Run Code Online (Sandbox Code Playgroud)

Par*_*han 7

为此你必须要数 device width and height runtime

int width = devicewidth/11 ;
int height =deviceheight/11 ;
Run Code Online (Sandbox Code Playgroud)

使用此参数您必须传入 GridView's Adapter

android.widget.AbsListView.LayoutParams parms = new android.widget.AbsListView.LayoutParams(width , height);
Run Code Online (Sandbox Code Playgroud)

调整其细胞大小.