与屏幕分辨率成比例地缩放CardView

时间:2019-07-07 04:27:39

标签: android cardview

我有一个带有三个矩形CardViews的活动。我已经设置了大小限制,以使它们根据屏幕分辨率而变化。 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),底部部分是所需的尺寸。

尽管我会尽力使帖子尽可能清晰,但这些图片绝对有助于理解我所面临的问题,因此请查看它们。

..

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

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

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>

1 个答案:

答案 0 :(得分:0)

您所体验的是屏幕分辨率与像素密度的概念差异。如here所述:

  

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

就您而言,与Nexus 5相比,Pixel的像素密度更高。

要向您展示差异,请使用此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编程生涯中大有帮助。