Android 如何使我的应用程序与所有屏幕大小兼容,而不为其创建不同的布局文件?

Android 如何使我的应用程序与所有屏幕大小兼容,而不为其创建不同的布局文件?,android,xml,android-studio,Android,Xml,Android Studio,我有一个线性的按钮布局,所以当我在不同的手机上运行应用程序时,一些按钮会被隐藏。因为我的应用程序在代码方面已经准备好了,所以我需要一些简短的技术,使它能够兼容所有屏幕大小 活动\u main.xml <?xml version="1.0" encoding="utf-8"?> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" xmlns

我有一个线性的按钮布局,所以当我在不同的手机上运行应用程序时,一些按钮会被隐藏。因为我的应用程序在代码方面已经准备好了,所以我需要一些简短的技术,使它能够兼容所有屏幕大小

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    tools:context="com.example.android.hiha.MainActivity"
    android:background="@drawable/main_activity_cover">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <include layout="@layout/tool_bar"
      android:id="@+id/toolbar"></include>

<RelativeLayout
    android:id="@+id/btn_list_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_below="@id/toolbar">
    <include
        layout="@layout/button_list"
        android:id="@+id/button_list"></include>
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/btn_list_layout"
    android:layout_alignParentBottom="true"
    android:gravity="bottom">

    <include
        layout="@layout/bottom_section_main"
        android:id="@+id/bottom_section_main"></include>

</RelativeLayout>
</RelativeLayout>
</ScrollView>
相对布局百分比

约束布局

相对布局百分比

约束布局

不要为按钮或文本视图提供任何固定大小 根据您提供的上述代码

<ImageView
        android:id="@+id/about_btn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/about"
        android:layout_gravity="center"
        android:elevation="4dp"/>``
``
图像视图您为其指定的固定大小为50dp(高度和宽度)。当您运行不同的手机时,图像视图的高度和宽度永远不会改变。
因此,大多数使用Wrap\u内容和Match\u Parent的方法不会为按钮或文本视图提供任何固定大小 根据您提供的上述代码

<ImageView
        android:id="@+id/about_btn"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/about"
        android:layout_gravity="center"
        android:elevation="4dp"/>``
``
图像视图您为其指定的固定大小为50dp(高度和宽度)。当您运行不同的手机时,图像视图的高度和宽度永远不会改变。
因此,大多数使用包装内容和匹配父项

使用布局权重。研究他们。我已经开始使用布局和重量,他们调整到任何屏幕大小。权重用于线性布局和表格布局。可能还有更多,但这是我用过的两个。如果要在项目之间留出空间,请转到布局文本屏幕并键入

<View

使用布局权重。研究他们。我已经开始使用布局和重量,他们调整到任何屏幕大小。权重用于线性布局和表格布局。可能还有更多,但这是我用过的两个。如果要在项目之间留出空间,请转到布局文本屏幕并键入

<View

如果我不提供固定大小,则按钮布局文件将失真!有四个分辨率屏幕1-:hdpi 2-:xhdpi 3-:xxhdpi 4-:xxxhdpi您可以为四个资源创建四个维度和可绘制文件夹。ex-hdpi--12dp(在dimen_hdpi中)xhdpi--14dp(在dimen_xhdpi中)xxhdpi--16dp(在dimen_xxhdpi中)xxxhdpi--18dp(在dimen_xxxhdpi中)因此,当您运行应用程序以获得比txt视图三种不同的资源时,自动获得高于dimen的大小如果我不提供固定大小,则按钮布局文件将失真!有四个分辨率屏幕1-:hdpi 2-:xhdpi 3-:xxhdpi 4-:xxxhdpi您可以为四个资源创建四个维度和可绘制文件夹。ex hdpi--12dp(在dimen_hdpi中)xhdpi--14dp(在dimen_xhdpi中)xxhdpi--16dp(在dimen_xxhdpi中)xxxhdpi--18dp(在dimen_xxxhdpi中)所以当您运行应用程序以获得比txt视图三种不同的资源时,会自动获得高于dimen的数据