简体   繁体   English

Android gridview行高根据显示

[英]Android gridview row height according to the display

and sorry for my english. 对不起,我的英语。 A simple question (if you know the answer :-D): I have a GridView with only images, all the images are same size. 一个简单的问题(如果您知道答案:-D):我有一个仅包含图像的GridView,所有图像的大小相同。 I have set the numbers of colums in xml. 我已经在xml中设置了列数。 Here is the code: 这是代码:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_view_bambini14"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:numColumns="@integer/grid_columns" <!-- grid_colums value is set in res/values, in case the display is landscape or portrait-->
        android:horizontalSpacing="10sp"
        android:verticalSpacing="10sp"
        android:gravity="center"
        android:stretchMode="columnWidth" >

And this is the java code: 这是java代码:

Public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT , 260));
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

the problem is the row height. 问题是行高。 In some smartphone the images are show in the right way, in other (I suppose according to resolution and density of the monitor) the row are too high. 在某些智能手机中,图像以正确的方式显示,而在其他智能手机中(我想根据显示器的分辨率和密度),行太高。 I tried to change this row 我试图改变这一行

imageView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT , 260));

with different values instead of 260, ando so to put GridView.AUTO_FIT, but the result is always obscene. 使用不同的值(而不是260),然后将GridView.AUTO_FIT放进去,但是结果总是令人讨厌。 How can I fix it? 我该如何解决? Thanks! 谢谢!

One possible problem of why the height appears differently on various devices is the way you specify the height here: 为什么高度在各种设备上显示不同的一个可能问题是您在此处指定高度的方式:

imageView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 260));

A good starting point to your problem would be using density pixels instead: 解决问题的一个很好的起点是改用密度像素:

imageView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, context.getResources().getDimension(R.dimen.grid_view_item_height)));

And specify grid_view_item_height in resources: 并在资源中指定grid_view_item_height:

<dimen name="grid_view_item_height">100dp</dimen>

Instead: 代替:

context.getResources().getDimension(R.dimen.grid_view_item_height) context.getResources()。getDimension(R.dimen.grid_view_item_height)

Try this: 尝试这个:

context.getResources().getDimensionPixelSize(R.dimen.grid_view_item_height) context.getResources()。getDimensionPixelSize(R.dimen.grid_view_item_height)

Because, in dimen.xml its in dp (float) . 因为在dimen.xml中它在dp(float)中 Thus, use getDimensionPixelSize instead of getDimension. 因此,请使用getDimensionPixelSize而不是getDimension。 It works fine. 工作正常。 KUDOS # 库多斯#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM