【问题标题】:Why my images and texts isn't being scaled on different screen sizes?为什么我的图像和文本没有在不同的屏幕尺寸上缩放?
【发布时间】:2017-08-17 06:01:03
【问题描述】:

我正在尝试支持不同的屏幕尺寸,如指南中所示:https://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts

我有 5 个不同的可绘制文件夹:

  • 可绘制(图像尺寸 40x48png)

  • drawable-mdpi(图片尺寸80x96png)

  • drawable-hdpi(图片尺寸120x144png)

  • drawable-xhdpi(图片尺寸160x192png)

  • drawable-xxhdpi(图片大小210x252png)

我的项目包括 5 个不同尺寸的布局文件夹: 300dp、330dp、480dp、600dp、720dp。每个文件夹都有相同的 layout.xml 文件。

我的布局代码:

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:layout_margin="15dp"
    android:text="Truuuuu"
    android:textColor="#000000"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.39"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/textView2"
    android:layout_marginTop="60dp"
    android:background="#ffffff"
    android:orientation="horizontal"
    android:id="@+id/relativeLayout">

    <ImageView
        android:id="@+id/off50"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:src="@drawable/off50" />

    <TextView
        android:id="@+id/akcija"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="pewpew!"
        android:textSize="20sp"
        android:layout_alignTop="@+id/off50"
        android:layout_alignParentEnd="true"
        android:layout_toEndOf="@+id/off50"
        android:textColor="#000000"/>

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/off50"
        android:layout_alignParentEnd="true"
        android:text="pewpew"
        android:textColor="#000000"
        android:textSize="20sp" />



</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="15dp"
    android:layout_alignStart="@+id/relativeLayout"
    android:layout_below="@+id/relativeLayout">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/idk"/>

</RelativeLayout>

我的问题是.. 我确实有不同的图像文件夹和图像文件,对吗?我也制作了布局文件夹。我的文本也没有缩放,例如在 layout-sw720dp 上,我几乎可以阅读文本。

我应该怎么做才能支持文本缩放?为什么图像没有在不同的屏幕尺寸上缩放?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    好吧,你很困惑。

    如果您在每个 sw 文件夹中都有相同的布局文件,请不要这样做。如果您对该尺寸有不同的屏幕布局,您只需要编写一个新的布局文件。请注意,这意味着全新或已删除的元素,或更改的位置。如果您只是更改值,则使用值文件。

    请记住,您链接的页面虽然是一个很好的资源,但它提到了您可以做的多项事情。你不应该做所有的事情,事实上其中一些是矛盾的。正确的做法取决于您希望事物的外观。

    对于您的图像缩放 - 您正在根据屏幕密度进行缩放。这意味着具有相同密度的所有设备都将使用相同的可绘制对象,这将使其看起来具有相同的物理尺寸,但不会相对于设备的尺寸进行缩放。这可能是也可能不是你想要的。如果不是,您可能希望在图像的 dp 中定义一个固定大小,或者将其设为 match_parent 并使用 scaleType 对其进行缩放。

    对于文本大小 - 您在任何地方都放置了 20sp 的固定文本大小。这将使它在所有设备上的高度相同。通常,在不同的屏幕尺寸上使文本变大并没有太大的作用。如果需要,请将 20sp 设为维度,并根据大小在 dimen.xml 文件中为其设置不同的值。

    为您提供更多帮助确实需要更多信息 - 您正在尝试实现什么以及您将获得什么。

    【讨论】:

    • 我明白了价值观部分的意义。我应该使用创建布局文件夹(sw)时相同的数字吗?我将图像特征更改为(宽度和高度 150dp,centerCrop scaleType 并且图像仍然无法缩放.. hmm
    • 是的。这些数字对我来说似乎相当合理。当然,他们都有点武断。我想说也许 300 vs 330 dp 并不太重要。
    • 谢谢!我设法修复了文本,现在我只剩下图像了.. 玩弄它的重点​​.. 仍然不能:/
    • 嘿@Gabe Sechan,我已将宽度和高度更改为 150dp,并且我已经尝试了不同的 scaleTypes.. 也许我在我的文件夹中做错了什么?
    • 因此设置 150 dp 大小意味着视图将始终为 150 dp - 始终约为 0.9 平方英寸,并且图像将根据 scaleType 在其中缩放。如果你想要一些其他效果(比如希望它占据某个元素之前的所有空间,或者总是占据屏幕的 10%),你需要使用其他技术
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    相关资源
    最近更新 更多