gpt4 book ai didi

java - 我的布局在低 dpi 设备上无法正确缩放

转载 作者:行者123 更新时间:2023-11-30 12:07:56 25 4
gpt4 key购买 nike

我制作了一个带有约束布局的 android 应用程序,即使我使用 SP 和 DP 作为单位,布局也无法正确缩放。

我仔细检查了所有值,它们都有 dp 和 sp,我在以下模拟器 5.1"、pixel、pixel 10"tablet、pixel 2、pixel 2 XL 和 nexus 7 上进行了尝试

我的布局实际上在这些上工作得很好,但在 dpi 较小的 nexus 4 上它被毁了。

编辑:即使将 cardview 约束设置为 match_parent,它仍然没有修复它,没有任何反应。

至于物理设备,它在我的手机上运行良好,具有 499 的高 dpi,但我的另外两部没有高 dpi 的手机,它被毁了。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:background="@drawable/withoutgrid">
<!--grid layout to hold two cards-->
<android.support.v7.widget.GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:paddingBottom="40dp"
app:columnCount="2"
app:columnOrderPreserved="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.755"
app:rowCount="1">
<!--first card layout-->
<android.support.v7.widget.CardView
android:id="@+id/cardView1"
android:layout_width="170dp"
android:layout_height="290dp"
android:layout_margin="12dp"
android:onClick="sPlayer"
app:cardBackgroundColor="@color/backgroundColor"
app:cardCornerRadius="30dp"
app:cardElevation="10dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1">

<ImageButton
android:id="@+id/singleplayer"
android:layout_width="150dp"
android:layout_height="170dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@null"
android:onClick="sPlayer"
android:paddingBottom="20dp"
android:scaleType="fitXY"
android:src="@drawable/robotfinal2" />

<TextView
android:id="@+id/singleplayertext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center_horizontal"
android:onClick="sPlayer"
android:paddingBottom="16dp"
android:text="Single Player"
android:textColor="@android:color/white"
android:textSize="24sp" />
</android.support.v7.widget.CardView>

</android.support.v7.widget.CardView>
<!--copy paste for second card layout-->

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

以下是 2 台相同设备的一些屏幕截图,一台具有自定义高 dpi,另一台具有标准 dpi

high dpi low dpi

Emulator

最佳答案

这里的一个问题是您正在小屏幕尺寸(例如大约 4.7 英寸的 Nexus 4)上测试布局。

您可能没有在最初设计时就专注于如此小的屏幕尺寸来开发应用程序。

考虑为不同的屏幕尺寸创建替代布局。

支持不同的屏幕尺寸会有所帮助。

查看此链接: Support different screen sizes

关于java - 我的布局在低 dpi 设备上无法正确缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54571547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com