gpt4 book ai didi

android - 根据屏幕分辨率按比例缩放 CardView

转载 作者:行者123 更新时间:2023-11-29 02:19:55 26 4
gpt4 key购买 nike

我有一个包含三个矩形 CardView 的 Activity 。我已经设置了尺寸限制,以便它们根据屏幕分辨率而变化。 Please view my constraints here.

The problem is that the CardViews are not changing their size proportionally. 例如,当我在 Nexus 5 (1080 x 1920) 上运行我的应用程序时,底部组件被切成两半,就像我在 Pixel(也是 1080 x 1920)上运行一样,底部组件是所需的大小。

虽然我会尽量使帖子尽可能清晰,但这些图片肯定有助于理解我面临的问题,所以请查看它们。

..

为什么屏幕尺寸非常相似时底部组件变化如此之大?我如何修改组件,使它们成为这些不同屏幕尺寸所需的尺寸?

我知道您可以创建小型、普通、大型和超大型布局,我也有,但我认为这不是问题所在。

3 个 CardView 的代码:

左上角

    <android.support.v7.widget.CardView
android:id="@+id/capture_receipt_cardview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="@dimen/margin_width_normal"
android:layout_marginEnd="@dimen/button_gap_normal"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:layout_constraintBottom_toBottomOf="@+id/create_invoice_cardview"
app:layout_constraintEnd_toStartOf="@+id/create_invoice_cardview"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/create_invoice_cardview">
</android.support.v7.widget.CardView>

右上角:

    <android.support.v7.widget.CardView
android:id="@+id/create_invoice_cardview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="@dimen/button_gap_normal"
android:layout_marginTop="185dp"
android:layout_marginEnd="@dimen/margin_width_normal"
android:layout_marginBottom="@dimen/button_gap_normal"
app:layout_constraintBottom_toTopOf="@+id/add_single_cardview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/capture_receipt_cardview"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.CardView>

水平底部:

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/add_single_cardview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="400dp"
android:layout_marginBottom="68dp"
app:layout_constraintBottom_toTopOf="@+id/navigation"
app:layout_constraintEnd_toEndOf="@+id/create_invoice_cardview"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/capture_receipt_cardview"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:layout_constraintVertical_weight="0">
</android.support.v7.widget.CardView>

最佳答案

您遇到的问题是由屏幕分辨率与像素密度的概念差异造成的。如前所述 here :

The pixel density is the number of pixels within a physical area of the screen and is referred to as dpi (dots per inch). This is different from the resolution, which is the total number of pixels on a screen.

在您的案例中,Pixel 的像素密度高于 Nexus 5。

为了向您展示差异,请看这个 formula :

public int dpToPx(int dp) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

给定你的android:layout_marginBottom="68dp"

Nexus 5 手机的像素数:

102 = 68 * (240 / 160); // I am assuming the DPI is HIGH

Pixel 手机的像素数量:

136 = 68 * (320 / 160); // I am assuming the DPI is XHIGH   

了解这个概念对您的 Android 编程生涯大有帮助。

关于android - 根据屏幕分辨率按比例缩放 CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56919340/

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