android - 如何在不同的屏幕尺寸下设置交错网格布局。?

标签 android android-layout android-gridview android-cardview android-gridlayout

我正在使用交错网格布局管理器在我的应用程序中设置交错网格,我在“卡片 View ”中使用了 Imageview。交错效果正常工作,但在某些情况下,当图像尺寸太大时,布局将是像这样

[![在此处输入图片描述][1]][1]

我的xml文件

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="220dp"
android:layout_height="180dp"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="8dp"
android:layout_marginBottom="16dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/country_photo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/action_settings"
        android:src="@drawable/three"

        android:scaleType="centerCrop" />

    <ImageView
        android:id="@+id/outside_imageview"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginBottom="8dp"
        android:layout_marginRight="8dp"

        android:src="@drawable/varified"
        android:layout_alignBottom="@id/country_photo"

        android:layout_alignRight="@id/country_photo"
        android:scaleType="fitEnd" />
</RelativeLayout>

卡片 View 的宽度为 220dp,高度为 180dp, ImageView 宽度 = "match_parent"和高度 = "wrap_content"缩放类型也为 centerCrop,这适用于小尺寸图像。布局将是像下面的屏幕截图,如果是大尺寸图像。如何解决这个问题??

在小型设备上的 View 如下所示

最佳答案

为大屏幕布局、普通屏幕布局和小屏幕布局等设置不同的列数。使用此代码以编程方式检查设备屏幕尺寸。

public void checkScreenSize()
{

int screenSize = getResources().getConfiguration().screenLayout &
        Configuration.SCREENLAYOUT_SIZE_MASK;
String toastMsg;
switch (screenSize) {
        case Configuration.SCREENLAYOUT_SIZE_XLARGE:
            toastMsg = "XLarge screen";

            //set action

            break;
        case Configuration.SCREENLAYOUT_SIZE_UNDEFINED:
            toastMsg = "XLarge screen";
              //set action
             break;
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
            toastMsg = "Large screen";
             //set action
            break;

        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
            toastMsg = "Normal screen";
             //set action
            break;
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
             //set action
            toastMsg = "Small screen";
            break;
        default:
            toastMsg = "Screen size is neither large, normal or small";



}

关于android - 如何在不同的屏幕尺寸下设置交错网格布局。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31988315/

相关文章:

java - 基本蓝牙应用程序无法运行

android - 导入库仅用于调试

android - 如何使用android中的广播接收器来振动设备?

java - 为什么我的 GridView 在末尾中断?

android - Kotlin Android Firebase 数据库 Hashmap 转换为类

android - 设定时间来电通知

android - 如何将自定义 ActionBar 设置为 ScrollView 布局中的固定顶部位置?

android - 多种小部件尺寸

android - 与 AsyncTask 结合使用时处理 GridView 的并发

平板电脑的 Android "smallest width"布局无法正常工作