如何自动计算跨度和图标大小

如何自动计算跨度和图标大小

本文介绍了如何自动计算跨度和图标大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有卡片视图的回收站视图在我的应用程序中显示图标.我无法弄清楚如何根据屏幕尺寸自动调整应用程序图标的大小以及增加/减少跨度计数.这是我要显示的内容,并且spen计数固定为3.

I am using recycler view with card view to show icons in my application.I am not able to figure out how to resize the app icons and increase/decrease the span count automatically depending on the screen size. Here is what I am doing to display it and the spen count is fixed to 3.

    RecyclerView mrv = (RecyclerView) findViewById(R.id.recyclerview_id);
    RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this, lsStore);
    mrv.setLayoutManager(new GridLayoutManager(this, 3));
    mrv.setAdapter(myAdapter);

XML如下

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cardview_id"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:layout_marginVertical="20dp"
cardview:cardCornerRadius="6dp">


<android.support.v7.widget.LinearLayoutCompat
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageView
        android:id="@+id/store_image_id"
        android:layout_width="wrap_content"
        android:layout_height="110dp"
        android:scaleType="centerCrop"
        android:background="#2D2D2D"/>

    <TextView
        android:id="@+id/store_name_id"
        android:textColor="#2d2d2d"
        android:textSize="13sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/StoreTitle"/>


</android.support.v7.widget.LinearLayoutCompat>

该应用在屏幕尺寸约为5"5'至6"的屏幕上表现不错,但是在较小/较大的屏幕上,图标看起来很乱.有人可以指导我如何实现这一目标吗?

The app is doing nice in a screen size of around 5"5' to 6" screen but on screens smaller/bigger that, the icons looks messy. Can someone please guide me how to achieve this?

推荐答案

您可以计算列数.定义一个静态函数来计算列数为:

You can calculate number of columns. Define a static function to calculate the number of columns as:

public class Utility {
    public static int calculateNoOfColumns(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
        int noOfColumns = (int) (dpWidth / 180);
        // Where 180 is the width of your grid item. You can change it as per your convention.
        return noOfColumns;
    }
}

然后,在活动或片段中使用GridLayoutManager时,您可以执行以下操作:

And then, when using the GridLayoutManager in the activity or fragment you can do like this:

int mNoOfColumns = Utility.calculateNoOfColumns(getApplicationContext());

// Some code...

mGridLayoutManager = new GridLayoutManager(this, mNoOfColumns);
mRecyclerView.setLayoutManager(mGridLayoutManager);

// Some code...

希望对您有所帮助.

这篇关于如何自动计算跨度和图标大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:59