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

时间:2014-03-16 17:25:58

标签: android android-layout gridview

我使用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>

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

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>

activity_main.java

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

adapter.java

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

1 个答案:

答案 0 :(得分:7)

为此你必须计算device width and height runtime

int width = devicewidth/11 ;
int height =deviceheight/11 ;

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

android.widget.AbsListView.LayoutParams parms = new android.widget.AbsListView.LayoutParams(width , height);

用于调整其单元格的大小。