【发布时间】:2021-10-23 00:30:21
【问题描述】:
我正在尝试使用 GridLayout、CardView 和 LinearLayout 制作应用程序,因此在模拟器上一切正常,但是当我在真实设备上测试我的应用程序时,卡片视图超出屏幕我已经在不同设备上对其进行了测试,但问题是仍然出现请帮忙!
This is how it looks inside the emulator
This is how it looks on a real device
XML 代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/logo_img"
android:src="@drawable/logo"
android:layout_gravity="center"
android:layout_width="250dp"
android:layout_height="wrap_content"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:rowCount="4">
<androidx.cardview.widget.CardView
android:id="@+id/view1"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_margin="15dp"
app:cardBackgroundColor="#DE773C"
app:cardCornerRadius="12dp"
app:cardElevation="5dp">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:src="@drawable/img1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="TEST"
android:textAlignment="center"
android:textColor="#272932"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/view2"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_margin="15dp"
app:cardBackgroundColor="#DE773C"
app:cardCornerRadius="12dp"
app:cardElevation="5dp">
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:src="@drawable/img2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="TEST"
android:textAlignment="center"
android:textColor="#272932"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</LinearLayout>
</ScrollView>
【问题讨论】:
-
尝试将两个 CardView 的 layout_width 更改为 match_parent 或 wrap_content。
标签: android android-studio android-layout android-cardview android-gridlayout