Android 不会根据不同的屏幕布局调整大小

Android doesn't resize with different screen layouts

我正在尝试根据我使用的设备调整布局大小。 我正在测试两款不同的手机(三星 Galaxy S8+ 和索尼 Xperia Z2(甚至是两款不同的模拟器))。

我在清单文件中添加了一些代码。 我创建了具有正确(假定)名称的不同文件夹(layout、layout-small、layout-large、layout-xlarge)。 我将我的代码放入值文件夹,文件 "dimens.xml" 并从我的布局中调用它。 但似乎没有任何效果。

我的屏幕唯一一次发生变化是在我将相应文件修改到 "layout" 文件夹时。

我希望得到任何帮助或建议。谢谢!

我已经阅读了很多关于 Android Developer 和 Stack Overflow 的文档,但我似乎找不到解决方案。

AndroidManifest.xml

的片段
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.applicationpro">

    <supports-screens android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"
        android:resizeable="true" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />

res/layout-large/my_layout 的片段。xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLL"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg4"
    android:orientation="vertical"
    tools:context=".activity.LoginActivity">

    <ProgressBar
        android:id="@+id/loginPG"
        android:theme="@style/FragmentsProgressBar"
        style="@style/Widget.AppCompat.ProgressBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:layout_marginTop="100dp"/>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/loginTIY"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_top_large">

            <AutoCompleteTextView
                android:id="@+id/loginTV"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/prompt_login"
                android:maxLines="1"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>

dimens.xml


<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
    <dimen name="standard_55">65dp</dimen>
    <dimen name="standard_105">135dp</dimen>
    <dimen name="standard_155">155dp</dimen>

    <!-- Small Dimensions = "Medium Dimensions * 0.75" For Example: 210*.75 = 157.5-->
    <dimen name = "margin_top_small">150dp</dimen>

    <!-- Medium Dimensions -->
    <dimen name = "margin_top_medium">200dp</dimen>


    <!-- Large Dimensions = "Medium Dimensions * 1.5" For Example: 210*1.5 = 315 -->
    <dimen name = "margin_top_large">300dp</dimen>


    <!-- XLarge Dimensions = "Medium Dimensions * 2" For Example: 210*1.5 = 420 -->
    <dimen name = "margin_top_xLarge">400dp</dimen>

</resources>

TL;DR 这是另一种使用 ConstraintLayout 的方法,而不是针对每个屏幕尺寸

的单一布局

您可以使用ConstraintLayout支持不同的屏幕尺寸:

ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It's similar to RelativeLayout in that all views are laid out according to relationships between sibling views and the parent layout, but it's more flexible than RelativeLayout and easier to use with Android Studio's Layout Editor.

您可以使用这些属性以百分比指定您的视图大小:

app:layout_constraintWidth_percent=".5"
app:layout_constraintHeight_percent=".5"

例如,单个按钮的高度和宽度都等于屏幕的 50%:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


<Button
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintWidth_percent=".5"
    app:layout_constraintHeight_percent=".5"/>

</androidx.constraintlayout.widget.ConstraintLayout>

它看起来像这样:

我可以推荐使用 ConstraintLayout with guidelines and Chains 来支持不同的屏幕尺寸(除了我上面提到的 - app:layout_constraintWidth_percentapp:layout_constraintHeight_percent)。


乍一看,这可能需要大量工作,有些人可能想知道这是否真的值得付出努力,但这就是为什么我相信 ConstraintLayout 是构建 UI:

  • 真的很人性化

  • ConstraintLayout 非常简单易学。

  • 一旦你学会了它,你会发现你节省了大量的开发时间,因为制作你的UI非常快。

  • 约束布局是为了支持不同的屏幕尺寸,所以不需要为每个屏幕尺寸构建布局(这也连接到之前的优势 - 节省开发时间)。