ConstraintLayout 由 Android Studio 自动调整大小

ConstraintLayout automatically resized by Android Studio

我是 Android 的新手,在 Android Studio 中设计布局时遇到问题。我发现了许多相关问题,但其中 none 有效。 Android Studio 根据当前预览大小(Pixel、Nexus 7...)始终将约束值硬编码在 dp 中。

我在 XML 编辑器 中编写的代码(在我的 phone 上工作得很好):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <com.meanstreet.note.Slider
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.meanstreet.note.MainActivity">

        <RelativeLayout
            android:id="@+id/menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/bold"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:text="@string/bold_button" />

...

我转到“设计”选项卡后更改了代码(不适合我的 phone 屏幕):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <com.meanstreet.note.Slider
        android:id="@+id/slider"
        android:layout_width="344dp"
        android:layout_height="495dp"
        tools:context="com.meanstreet.note.MainActivity"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">

        <RelativeLayout
            android:id="@+id/menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/bold"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:text="@string/bold_button" />
...

Slider 是扩展 RelativeLayout 的自定义视图。

Slider 中的硬编码值取决于为 Android Studio 中的预览选择的 phone,现在视图不在我的 phone 中(如果选择 phone 更大)或右侧和底部的边距较大(如果选择的 phone 更小)。

这对我来说似乎很奇怪:我仍然可以避免转到“设计”选项卡,这样代码就不会被更改并且可以在任何设备上运行,但这很烦人。

为什么 Android Studio 会这样?我怎样才能使宽度和高度保持在 "match_parent",从而使任何 phone 的尺寸都合适?

在此先感谢您的帮助。

match_parent 不支持 ConstraintLayout 内的元素。相反,您可以这样做;

  • 将滑块的宽度设置为 0dp
  • 使用这些属性向左侧和右侧添加约束。

    app:layout_constraintLeft_toLeftOf="parent"

    app:layout_constraintRight_toRightOf="parent"

这样您就可以水平拉伸该视图。你可以用同样的逻辑垂直地做同样的事情。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">

    <com.meanstreet.note.Slider
        android:id="@+id/slider"
        android:layout_width="0dp"
        android:layout_height="495dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:context="com.meanstreet.note.MainActivity"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">

        <RelativeLayout
            android:id="@+id/menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/bold"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:text="@string/bold_button" />