Android多屏幕尺寸的相对比例

Android多屏幕尺寸的相对比例,android,dimension,Android,Dimension,我想知道如何使android应用程序支持多种屏幕尺寸。我开发了一款应用程序,在我7英寸的平板电脑上看起来不错,但当我在android手机上运行同一款应用程序时(屏幕要小得多),一切都变得不成比例(对齐、大小)。谁能帮我解决这个问题。下面是我的应用程序屏幕上的代码,上面有一些按钮和文本。它在平板电脑上看起来不错,但在手机上却不行。有谁能对下面的代码做些修改,让它至少成为我的一个起点 my.xml文件中的一个片段 <LinearLayout xmlns:android="http://sche

我想知道如何使android应用程序支持多种屏幕尺寸。我开发了一款应用程序,在我7英寸的平板电脑上看起来不错,但当我在android手机上运行同一款应用程序时(屏幕要小得多),一切都变得不成比例(对齐、大小)。谁能帮我解决这个问题。下面是我的应用程序屏幕上的代码,上面有一些按钮和文本。它在平板电脑上看起来不错,但在手机上却不行。有谁能对下面的代码做些修改,让它至少成为我的一个起点

my.xml文件中的一个片段

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/theme">

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="50dp">

<TextView
        android:id="@+id/scale_tv"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="65dp"
        android:layout_width="100dp"
        android:layout_height="35dp" />
</LinearLayout>

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="5dp">
<Button
        android:id="@+id/scales"
        android:layout_width="115dip"
        android:layout_height="130dp"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/scale_home_btn"
        android:layout_marginTop="0dp" />
</LinearLayout>
</LinearLayout>

有两种选择

灵活的布局

你有很多硬编码的尺寸,使得你的布局不灵活。使用边距和权重,以便布局可以根据框架进行缩放

多个布局

一个选项是为不同的屏幕大小创建不同的布局。 只需创建多个资源文件夹,每个文件夹中都有一个同名的布局文件

  • res/布局小
  • 分辨率/布局大
  • res/layout xlarge
编辑

如果您希望对布局的缩放方式进行大量控制,则此选项非常有用。它通常不是首选选项,除非您希望组件以不同的方式排列(即平板电脑和手机布局),因为它需要维护多个布局文件

简化布局 顺便说一下,为了达到同样的效果,您的布局可以简单得多

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/theme">


    <TextView
        android:id="@+id/scale_tv"
        android:layout_marginTop="50dp"
        android:layout_width="100dp"
        android:layout_height="35dp" />


    <Button
        android:id="@+id/scales"
        android:layout_width="115dip"
        android:layout_height="130dp"
        android:layout_marginLeft="60dp"
        android:background="@drawable/scale_home_btn"
        android:layout_marginTop="5dp" />

</LinearLayout>


您能否添加更完整的布局(您是使用线性布局还是相对布局?)以及手机和平板电脑上的屏幕截图来说明问题?另外请注意,如果您将版面文件(同名)放入layout port/和layout land/(请参阅)@Marc,则不需要if/then/else构造。我已对.xml文件进行了一些更改,关于图像,此网站不允许我上载,并说“您需要至少10个声誉才能发布图像”。非常感谢您的回复。谢谢Cyroxis。你能告诉我什么工作实例,让我更容易理解吗?我会的grateful@user238710你能更具体地说明你想要什么吗?我建议阅读关于支持不同大小屏幕的链接,并提出澄清问题。我需要一个工作示例,无论它有多小,这样我就可以使用它,并在我的设备上实际看到变化。我已经浏览了您提供的链接,谢谢,但我认为它们主要是概念,我不明白如何在我的代码中使用它们。我需要在一些应用程序/代码中看到这些概念,这对我来说更容易理解。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/theme">


    <TextView
        android:id="@+id/scale_tv"
        android:layout_marginTop="50dp"
        android:layout_width="100dp"
        android:layout_height="35dp" />


    <Button
        android:id="@+id/scales"
        android:layout_width="115dip"
        android:layout_height="130dp"
        android:layout_marginLeft="60dp"
        android:background="@drawable/scale_home_btn"
        android:layout_marginTop="5dp" />

</LinearLayout>