首页 > 解决方案 > 如何更改 xml 文件中的可伸缩性

问题描述

这是我的屏幕尺寸为 5.0 和 4.0 的应用程序。如何在任何地方以相同的方式更改比例。

5.0

在此处输入图像描述

4.0

在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tv_sprawdz"
        android:layout_width="match_parent"
        android:layout_height="225dp"
        android:layout_marginTop="15dp"
        android:gravity="center_horizontal"
        android:text="Wybierz dzień"
        android:textAlignment="center"
        android:textColor="@color/White"
        android:textSize="18sp"
        android:textStyle="italic" />


    <LinearLayout

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/bg_celen">

        <CalendarView

            android:id="@+id/calendarView"
            android:layout_width="350dp"
            android:layout_height="537dp">

        </CalendarView>
    </LinearLayout>

</LinearLayout>

标签: androidandroid-layoutscalescreen-resolutionandroid-calendar

解决方案


这是一个常见的问题。请阅读下面我从Android Developer Docs中提取的内容

为了确保您的布局灵活并适应不同的屏幕尺寸,您应该使用“wrap_content”和“match_parent”作为大多数视图组件的宽度和高度,而不是硬编码的尺寸。

“wrap_content” 告诉视图将其大小设置为适合该视图中的内容所需的大小。

"match_parent" 使视图尽可能地扩展到父视图中。

例如:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/lorem_ipsum" />

尽管此视图的实际布局取决于其父视图和任何同级视图中的其他属性,但此 TextView 打算将其宽度设置为填充所有可用空间(match_parent)并将其高度设置为与文本(wrap_content)。这允许视图适应不同的屏幕尺寸和不同的文本长度。

此外,您还可以为不同的屏幕尺寸使用不同的布局,如下所述。

您可以通过创建额外的 res/layout/ 目录来提供特定于屏幕的布局 - 一个用于需要不同布局的每个屏幕配置 - 然后将屏幕配置限定符附加到布局目录名称(例如 layout-w600dp 用于具有 600dp 的屏幕可用宽度)。

这些配置限定符代表您的应用程序 UI 可用的可见屏幕空间。当从您的应用程序中选择布局时,系统会考虑任何系统装饰(例如导航栏)和窗口配置更改(例如当用户启用多窗口模式时)。

 res/layout/main_activity.xml # For handsets (smaller than 600dp available Width) 
 res/layout-w600dp/main_activity.xml # For 7” tablets
 or any screen with 600dp # available width (possibly landscape handsets)

推荐阅读