[Android Studio:CardView网格+内容无法在设备上以纵向模式正确显示

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

我正在Android Studio 3.5.3上使用androidx.cardview:cardview:1.0.0。Android Studio中我的activity_main布局的设计预览完美显示了布局OnPreview

但是,当我在设备(Huawei p30 lite)上对其进行测试时,该设计在纵向模式下都被弄乱了,根本没有显示任何内容

OnDevice

[在设备上以横向模式查看布局时,它可以完美显示设计,如Android Studio预览所示。

这是我的布局代码:

    <ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="40dp"
            android:layout_marginRight="20dp">

            <ImageView
                android:id="@+id/logo"
                android:layout_centerHorizontal="true"

                android:layout_width="90dp"
                android:layout_height="90dp"
                android:src="@drawable/circle"
                />
        </RelativeLayout>

        <GridLayout
            android:id="@+id/fam"
            android:layout_marginTop="40dp"
            android:columnCount="2"
            android:rowCount="3"
            android:alignmentMode="alignMargins"
            android:columnOrderPreserved="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                app:cardElevation="60dp"
                app:cardCornerRadius="12dp"
                android:layout_margin="12dp"
                >
                <LinearLayout
                    android:orientation="vertical"
                    android:padding="16dp"
                    android:gravity="center"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ImageView
                        android:src="@drawable/object"
                        android:layout_width="80dp"
                        android:layout_height="80dp">
                    </ImageView>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dp"
                        android:fontFamily="casual"
                        android:text="Object Detection"
                        android:textColor="#000000"
                        android:textSize="18sp">
                    </TextView>
                </LinearLayout>
                  </androidx.cardview.widget.CardView>

                  **repeat the above cardview for the remaining 5 cards**
                  .
                  </GridLayout>
        </LinearLayout>
</ScrollView>

[compilesdkversion为28,minsdkversion为23。

我也尝试过在android:screenOrientation中将portrait设置为AndroidManifest.xml,然后在我的设备上运行它,但还是没运气。

任何帮助将不胜感激!

android android-layout android-cardview androidx
2个回答
0
投票

您在Android Studio上的预览设备应与手机上的确切分辨率匹配。

您的华为设备分辨率为1080 x 2312像素。

检查您在Android Studio中预览设备的分辨率

像这样的AS preview,或者您可以创建与设备电话相同的自定义分辨率来进行修复。


0
投票

所以我设法解决了。在每个<LinearLayout>内的<CardView>中,我将android:layout_widthandroid:layout_height更改为wrapcontent,现在它在纵向模式下显示一切正常。

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