Android gridview 调整到屏幕大小 [英] Android gridview adjusting to screen size

查看:18
本文介绍了Android gridview 调整到屏幕大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的网格视图有 3 行和 3 列.无论设备的屏幕大小如何,我都希望它填满屏幕.

The grid view of my application has 3 rows and 3 columns. I want this to fill the screen ,irrespective of the screen size of the device.

我尝试获取窗口大小并相应地设置 layoutparams.但这并没有给出完美的对齐,因为它与线性布局中的权重一起工作.

I tried getting window size and setting the layoutparams accordingly. But this is not giving a perfect alignment as it works with weightsum in linearlayout.

那么我们可以将 weightsum 用于 gridview 还是可以使用任何其他 android 属性.非常感谢您的时间和回复.

So can we use weightsum for gridview or is there any other android property that can be used. Thanks a lot for time and response.

<GridView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/gridview"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/text1view"
                android:background="@drawable/home_bg"
                android:gravity="center"
                android:horizontalSpacing="10dip"
                android:numColumns="3"
                android:stretchMode="columnWidth" />

每个网格项是

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/grid_item_image"
    android:layout_width="wrap_content"
    android:layout_height="75dip"
    android:layout_margin="0dip"
    android:padding="0dip" >
</ImageView>

<TextView
    android:id="@+id/grid_item_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/grid_item_image"
    android:gravity="center_horizontal"
    android:padding="0dip"
    android:textColor="#000000"
    android:textSize="10sp" >
</TextView>

适配器代码:

    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.griditems, null);
        holder = new ViewHolder();
        holder.text1 = (TextView) convertView
                .findViewById(R.id.grid_item_text);

        holder.image = (ImageView) convertView
                .findViewById(R.id.grid_item_image);

        // if it's not recycled, initialize some attributes
        holder.image.setImageResource(gridItemIds[position]);
        holder.text1.setText(gridTitles[position]);
        int h = mContext.getResources().getDisplayMetrics().densityDpi;

        // holder.image.setLayoutParams(new LayoutParams(h-50,h-50));
        convertView.setLayoutParams(new GridView.LayoutParams(h - 45,
                h - 39));
        // holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        // holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
        // holder.image.setPadding(20, 20, 20, 20);

        // convertView.setLayoutParams(new GridView.LayoutParams(Utils
        // .getLayoutParameter(), Utils.getLayoutParameter()+50));

        holder.image.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                new ModuleManager().startModuleACtivity(position, mContext);
            }
        });
        convertView.setTag(holder);
    } else {

        holder = (ViewHolder) convertView.getTag();

    }

    return convertView;
}

static class ViewHolder {
    TextView text1;
    ImageView image;

}

请看这条线

convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));

我在不同大小的模拟器上进行了一些试验,得出了这个值.但我不认为这是定义高度和宽度的正确方法.

i arrived at this value with some trials on different size emulator. But i dont think its a right way to define height and width.

推荐答案

据我了解您的问题,由于您还没有发布任何代码,您想要做的是,您希望 gridview 全屏显示.

As far as i understood your question, as you have not posted any code, what you are trying to do is, you want gridview to appear on full screen.

首先,您必须使用一些布局来放置您的 gridview.执行以下操作:

Firstly you must be using some layout to put your gridview in. Do the following:

制作布局的高度和宽度以填充父级:

Make height and width of the layout to fill parent:

android:layout_width="fill_parent"
android:layout_height="fill_parent"

然后,将 gridview 的高度和宽度也设置为 fill_parent.

then, set height and width of your gridview to fill_parent as well.

好吧,这只是您可能一直在以这种方式工作的假设解决方案.放一些代码会好得多.希望这对你有用.

Well, this is just a supposed solution that you might have been working this way. putting some code would be much better. Hope this works for you.

更新:那么问题可能出在这里:

Update: Well the problem may lie here:

 convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));

改为:

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
 convertView.setLayoutParams(new GridView.LayoutParams(params));

希望这对你有用.

这篇关于Android gridview 调整到屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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