如何自动调整GridView的大小

时间:2015-06-18 15:18:37

标签: java android android-layout gridview

如何使GridView自动调整大小以达到屏幕分辨率。我在虚拟设备中看起来不错,但在手机上却看不到。我尝试了很多设置但没有任何作用。

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


            <GridView

             android:layout_centerHorizontal="true"
             android:layout_centerVertical="true"
             android:orientation="vertical"      
             android:id="@+id/gridview"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:numColumns="3"
             android:stretchMode="columnWidth"
             android:padding="5dp"
             android:verticalSpacing="5dp"
             android:horizontalSpacing="10dp"        
             android:gravity="center_vertical"
             android:layout_marginBottom="100dp"/> 

    </RelativeLayout>

来自ImageAdapter的getView:

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(R.drawable.card_back);
    return imageView;
}

虚拟设备: Virtual device

电话 enter image description here

2 个答案:

答案 0 :(得分:3)

问题不在于gridview,问题在于imageview本身。

正如您所看到的,您的网格实际上占据了屏幕并使用columnswidth填充了额外的空间。在您的情况下,您必须将图像放在中心。要执行此操作,请为imageview提供一个父级,例如linearlayout,并将imageview对齐到父级中心。

LinearLayout lLayout=new LinearLayout(context);
lLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lLayout.setGravity(Gravity.CENTER);
lLayout.addView(<your imageview>);

在适配器getview中使用上面的代码

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    LinearLayout lLayout;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
    lLayout=new LinearLayout(context);
    lLayout.setLayoutParams(new LinearLayout.LayoutParams   (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    lLayout.setGravity(Gravity.CENTER);

        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(8, 8, 8, 8);

    lLayout.addView(imageView);
    } else {
        lLayout= (LinearLayout) convertView;
    }


  ((ImageView)lLayout.getChildAt(0)).setImageResource(R.drawable.card_back);

    return lLayout;
}

答案 1 :(得分:1)

   imageView.setLayoutParams(new GridView.LayoutParams(200, 200));

这是200dx而不是dp,为什么。 因此,在Values文件夹中创建dimension.xml,如

<resources>
 <dimen name="gridview">200dp</dimen>
</resources>

然后

 int size = mContext.getResources().getDimensionPixelSize(R.dimen.gridview);
 imageView.setLayoutParams(new GridView.LayoutParams(size, size));