Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ConstraintLayout由Android Studio自动调整大小_Android_Android Layout_Android Studio_Android Constraintlayout - Fatal编程技术网

ConstraintLayout由Android Studio自动调整大小

ConstraintLayout由Android Studio自动调整大小,android,android-layout,android-studio,android-constraintlayout,Android,Android Layout,Android Studio,Android Constraintlayout,我是Android新手,在Android Studio中设计布局时遇到问题。我发现了许多相关的问题,但没有一个有效。Android Studio始终根据当前预览大小(像素、Nexus 7…)在dp中硬编码约束值 我在XML编辑器中编写的代码(这在我的手机上很好): ... 在我转到“设计”选项卡后,代码发生了更改(这不适合我的手机屏幕): ... 滑块是扩展RelativeLayout的自定义视图 滑块中的硬编码值取决于Android Studio中预览所选的手机,并且视图现在不在“我的

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

我在XML编辑器中编写的代码(这在我的手机上很好):


...
在我转到“设计”选项卡后,代码发生了更改(这不适合我的手机屏幕):


...
滑块是扩展RelativeLayout的自定义视图

滑块中的硬编码值取决于Android Studio中预览所选的手机,并且视图现在不在“我的手机”范围内(如果所选手机较大),或者在右侧和底部有较大的边距(如果所选手机较小)

这对我来说似乎很奇怪:我仍然可以避免进入设计选项卡,这样代码就不会被更改,并且可以在任何设备上工作,但这很烦人


为什么安卓工作室会这样?我怎样才能拥有“match_parent”的宽度和高度,从而在任何手机上拥有合适的尺寸


提前感谢您的帮助。

match\u parent
不支持
ConstraintLayout
中的元素。相反,你可以这样做

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

    app:layout\u constraintLeft\u toLeftOf=“parent”

    app:layout\u constraintRight\u 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" />


“我发现了许多相关的问题,但没有一个有效。”你发现了哪些相关问题?当您尝试建议的解决方案时,结果如何?您想要的结果与这些结果有什么不同?“我怎样才能使宽度和高度保持在“匹配父项”?“只需将宽度和高度设置为“匹配父项”。=>表示值不是用代码编译的,但似乎是这样。=>使用0dp时,不显示滑块。=>我试着推断常数,但结果更糟,屏幕大小的一半是空白inserted@Code-学徒我一进入设计选项卡,这些值就会被dp中的值替换,如我发布的代码所示:这就是问题所在,它试图让您使用约束,因为这就是布局设计的目的。如果您不熟悉ConstraintLayout,欢迎您根据需要更改约束布局。但是,一般来说,“开始”和“结束”应该优先于“左”和“右”来支持所有区域设置。谢谢您的帮助。我可以将这些参数设置为ConstraintLayout,但是我应该为滑块设置什么呢?仍然匹配父项?无论我为左、右、开始、结束设置了什么约束,似乎都不起作用。。。ConstraintLayout的大小保持不变zero@Mean-实际上,这些都是用于滑块的。ConstraintLayout应该保持匹配父对象,匹配父对象。好吧,这样行!我认为“ConstraintLayout不支持match_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="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" />
...
<?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" />