使用GridLayout在每个设备中显示Cardviews的SAM大小

时间:2019-08-16 16:48:11

标签: android android-studio androidx android-gridlayout

我使用GridLayout在一个活动中显示4张卡片视图,但是问题是大小在不同的设备大小之间变化。如何使用GridLayout在每个设备上显示相同大小的卡片视图?现在他们被抓了。

这是我要在每台设备上显示Cardviews的图片:

Picture 1

Picture where the size of card views changes

这是我的GridLayout:

<androidx.gridlayout.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="6"
    app:columnCount="2"
    app:rowCount="2"
    app:alignmentMode="alignMargins"
    app:columnOrderPreserved="false"
    android:padding="10dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/btn_kategorie_1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:cardCornerRadius="20dp"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:elevation="7dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Geschichte"
                android:textSize="25sp"
                android:layout_gravity="center_horizontal"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/geschichte"
                android:layout_gravity="center" />

        </LinearLayout>


    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/btn_kategorie_2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:cardCornerRadius="20dp"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:elevation="7dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sport"
                android:textSize="25sp"
                android:layout_gravity="center_horizontal"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/sport"
                android:layout_gravity="center"/>

        </LinearLayout>


    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/btn_kategorie_3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:cardCornerRadius="20dp"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:elevation="7dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Musik"
                android:textSize="25sp"
                android:layout_gravity="center_horizontal"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/musik" />

</androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/btn_kategorie_4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:elevation="7dp"
        app:cardCornerRadius="20dp"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Wirtschaft"
                android:textSize="25sp"
                android:layout_gravity="center_horizontal"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/geld"
                android:layout_gravity="center"
                android:scaleType="fitCenter"/>

        </LinearLayout>


    </androidx.cardview.widget.CardView>

</androidx.gridlayout.widget.GridLayout>

1 个答案:

答案 0 :(得分:0)

这里有两种解决方法

  • 首先使用recyclerView适配器,仅制作一张卡片视图,并将网格布局管理器交给recyclerView
  • 如果从此代码转到第二个,则使用此库
  

实现'com.intuit.sdp:sdp-android:1.0.6'