具有网格布局的Android Cardview固定大小

问题描述 投票:0回答:1

我想获得一个包含图像和一些TextView的固定尺寸Cardview。这是XML:

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


<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"

tools:context=".ProductListFragment">

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Outlet : Head Office"
        android:textSize="18sp"
        android:layout_marginTop="24dp"
        android:layout_marginStart="32dp"/>

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:hint="  Find Product"
        app:helperTextEnabled="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableStart="@drawable/search_icon"
            android:drawableLeft="@drawable/search_icon" />

    </com.google.android.material.textfield.TextInputLayout>


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        >

        <GridLayout
            android:paddingLeft="18dp"
            android:paddingRight="18dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alignmentMode="alignMargins"
            android:columnCount="2"
            android:columnOrderPreserved="false"
            android:rowCount="2"

            >

            <com.google.android.material.card.MaterialCardView
                style="@style/CardView"
                android:id="@+id/sampleProductCard"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_margin="12dp"
                app:cardCornerRadius="12dp"
                app:cardElevation="6dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:paddingBottom="8dp"
                    >

                    <ImageView
                        android:layout_width="80dp" //This is what i change from 80 dp to match parent
                        android:layout_height="80dp"
                        android:src="@drawable/you_win"
                        />


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_marginLeft="12dp"
                        android:layout_marginRight="12dp"
                        >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="12dp"
                            android:text="Apple Pie"
                            android:textColor="#6f6f6f"
                            android:textSize="14sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="12dp"
                            android:text="Rp. 40000"
                            android:textColor="#6f6f6f"
                            android:textSize="12sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:text="Stock : 4"
                            android:textColor="#000"
                            android:textSize="10sp"
                            />
                    </LinearLayout>



                </LinearLayout>

            </com.google.android.material.card.MaterialCardView>

            <com.google.android.material.card.MaterialCardView
                style="@style/CardView"
                android:id="@+id/sampleProductCard2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_margin="12dp"
                app:cardCornerRadius="12dp"
                app:cardElevation="6dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="vertical"

                    android:paddingBottom="8dp"
                    >

                    <ImageView
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:src="@drawable/you_win" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_marginLeft="12dp"
                        android:layout_marginRight="12dp"
                        >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="12dp"
                            android:text="Apple Pie"
                            android:textColor="#6f6f6f"
                            android:textSize="14sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="12dp"
                            android:text="Rp. 40000"
                            android:textColor="#6f6f6f"
                            android:textSize="12sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="4dp"
                            android:text="Stock : 4"
                            android:textColor="#000"
                            android:textSize="10sp"
                            />
                    </LinearLayout>

                </LinearLayout>

            </com.google.android.material.card.MaterialCardView>

        </GridLayout>
    </ScrollView>

</LinearLayout>


</layout>

它将产生结果:enter image description here

我想要的是图像具有与CardView相同的宽度。但是,当我将图像宽度更改为match_parent时,结果是:

enter image description here

我想念什么吗?如果您想了解更多信息,请告诉我。

编辑:我试图使每张卡都像this

android android-layout kotlin android-cardview
1个回答
0
投票

尝试实施->

© www.soinside.com 2019 - 2024. All rights reserved.