【问题标题】:Scale CardView proportionally to screen resolution根据屏幕分辨率按比例缩放 CardView
【发布时间】:2019-11-17 01:10:42
【问题描述】:

我有一个包含三个矩形 CardView 的活动。我已经设置了尺寸约束,以便它们根据屏幕分辨率而变化。 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),底部组件是所需的大小。

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

..

当屏幕尺寸非常相似时,为什么底部组件会发生如此剧烈的变化?如何修改组件以使它们成为这些不同屏幕尺寸的所需尺寸?

我知道您可以创建 small、normal、large 和 xlarge 布局,我也有,但我认为这不是问题所在。

3 CardViews 的代码:

左上角

    <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>

【问题讨论】:

    标签: android cardview


    【解决方案1】:

    您所遇到的情况是由屏幕分辨率与像素密度概念的差异带来的。如前所述here

    像素密度是屏幕物理区域内的像素数,称为 dpi(每英寸点数)。这与分辨率不同,分辨率是屏幕上的像素总数。

    在您的情况下,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 编程生涯大有帮助。

    【讨论】:

    • 好的,我看到手机有不同的像素密度。但是我怎样才能用它来获得正确的尺寸呢?搞砸之后,我注意到 Nexus 5 实际上是 480,而 Pixel 是 420,分别给出了 204 和 179 的值。但这并没有提供所需的输出。我试图以编程方式修改下边距,它确实改变了大小 - 但它仍然不是一个通用的解决方案,因为我需要将它更改为每个屏幕的不同值。即将其更改为 dpToPx 值的大小仍然变化很大
    • 不要使用我提到的函数将你的 DP 值转换为 PX。它只是向您展示DP的想法。始终使用 DP 值。
    • 好的,那么您将如何更改元素的大小以使其对每个屏幕都正确?这是使用 XML 还是以编程方式完成的?您可以提供帮助的任何资源 - 我似乎在任何地方都找不到。
    • 我要问的是,如果我的下边距设置为 68dp,为什么它不自动缩放?这不是dp的重点吗?
    • 这可能是您的两个设备被归类在同一个存储桶中:xxhdpi。显然,它们没有相同的像素密度。因此差异。在此处阅读更多信息:captechconsulting.com/blogs/…
    猜你喜欢
    • 2017-06-11
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 2016-04-02
    • 2021-06-03
    • 1970-01-01
    相关资源
    最近更新 更多