CardView没有在Android设备上显示(api 21及以下)

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

嗨我在我的应用程序中使用cardviews创建一个家庭仪表板设计,问题是,在api 21(Android 5.1)中,cardview没有显示。我尝试在Android清单中加入硬件加速但仍然无法正常工作。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f9ff"
    tools:context=".activity.MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="385dp"
        android:layout_height="273dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/header_img"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"
        android:layout_marginTop="29dp"
        app:srcCompat="@drawable/ukssssssss"
        tools:layout_editor_absoluteX="142dp"
        tools:layout_editor_absoluteY="93dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView2"
        android:layout_centerHorizontal="true"
        android:text="UKS TK CENDEKIA HARAPAN"
        android:textColor="#fff"
        tools:layout_editor_absoluteX="102dp"
        tools:layout_editor_absoluteY="193dp" />

    <GridLayout
        android:id="@+id/mainGrid"
        android:layout_width="383dp"
        android:layout_height="293dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="35dp"
        android:layout_weight="8"
        android:alignmentMode="alignMargins"
        android:columnCount="2"
        android:padding="14dp"
        android:rowCount="2"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="239dp">

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_rowWeight="1"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="125dp"
                    android:layout_height="55dp"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/medicalrecord"

                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Medical Record"

                    android:textColor="@android:color/black"
                    android:textSize="12sp"
                    android:textStyle="bold" />


            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_rowWeight="1"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="125dp"
                    android:layout_height="55dp"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/plant" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Nusantara Plants"

                    android:textColor="@android:color/black"
                    android:textSize="12sp"
                    android:textStyle="bold" />


            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_rowWeight="1"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="125dp"
                    android:layout_height="55dp"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/healingtherapy" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Healing Therapy"

                    android:textColor="@android:color/black"
                    android:textSize="12sp"
                    android:textStyle="bold" />


            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_rowWeight="1"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center_vertical"
                android:layout_margin="16dp"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="125dp"
                    android:layout_height="55dp"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/calendaricon"

                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="What's Going On?"
                    android:textAlignment="center"
                    android:textColor="@android:color/black"
                    android:textSize="12sp"
                    android:textStyle="bold" />


            </LinearLayout>
        </android.support.v7.widget.CardView>

    </GridLayout>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="12dp"
        android:text="Indonesia Sehat"
        android:textColor="#fff"
        android:textSize="18sp"
        android:textStyle="bold" />

</RelativeLayout>

尝试不同的Android版本,但它的工作原理,并在设计视图中我将其更改为api 21,它也显示黑色cardview而不是白色。关于this linkdoesnt的问题解决了我的问题,他们要求解决卡片提升,而不是卡片未显示

java android android-studio android-cardview android-gridview
5个回答
1
投票

首先添加Gradle构建文件:

implementation 'com.android.support:gridlayout-v7:28.0.0'

然后将网格布局更改为androidx.gridlayout.widget.GridLayout,并且必须从android:更改为app:


0
投票

您的代码包含与API 22及更高版本兼容的CardView属性。由于这个原因,CardView无法显示。

这是CarView的那些属性。

android:layout_columnWeight="1"
android:layout_rowWeight="1"

你应该查看这部分。


0
投票

Dude与你的代码简单无关,添加gradle库

在Gradle(app)中添加以下库:

implementation 'com.android.support:cardview-v7:26.1.+'
  • 我只需将您的代码复制并粘贴到我的Android Studio中即可。之后,我将添加上面的库,它给我一个以下输出:

enter image description here


0
投票

在ManiFest中放入android:hardwareAccelerated =“false”


0
投票

如果CardView驻留在GridLayout中,将显示这两个属性。

<GridLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:rowCount="3"
       android:columnCount="2"
       android:alignmentMode="alignMargins"
       android:columnOrderPreserved="false">

               <android.support.v7.widget.CardView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:elevation="6dp"
                        app:cardCornerRadius="6dp"
                        app:cardElevation="6dp"
                        android:layout_columnWeight="1"
                        android:layout_rowWeight="1"
                        >



            </android.support.v7.widget.CardView>
</GridLayout>

-1
投票

尝试使用android.support.v7.widget.GridLayout而不是GridLayout并改变

android:layout_columnWeight="1"
android:layout_rowWeight="1"

cardview里面的属性

app:layout_columnWeight="1"
app:layout_rowWeight="1"
© www.soinside.com 2019 - 2024. All rights reserved.