Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.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
Android 约束调整页边距_Android_Android Constraintlayout - Fatal编程技术网

Android 约束调整页边距

Android 约束调整页边距,android,android-constraintlayout,Android,Android Constraintlayout,我有一个简单的布局 <?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" android:layout_width="match_

我有一个简单的布局

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorDarkGray">


    <RelativeLayout
        android:id="@+id/container3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:alpha="0.7"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Another text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Some text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

正如您所看到的,没有左右边距。上下页边距都很好。如果我删除左边距或右边距,我的相对布局会稍微超出屏幕。

我想我完全可以不使用相对布局,但我很感兴趣为什么会发生这种情况。

尝试将您的
相对布局宽度更改为
android:layout\u width=“0dp”

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorDarkGray">


    <RelativeLayout
        android:id="@+id/container3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:alpha="0.7"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Another text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:fontFamily="sans-serif"
            android:letterSpacing="0.03"
            android:text="Some text"
            android:textColor="@color/colorWhite"
            android:textSize="11sp"
            android:textStyle="normal" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

是的,当您定义
相对长度时,布局中当然没有左右边距,如下所示:

<RelativeLayout
    android:id="@+id/container3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"

应避免在ConstraintLayout中使用嵌套视图。原因是:

  • ConstraintLayout允许您创建具有平面视图层次结构(无嵌套视图组)的大型复杂布局,如中所述

  • 如中所述,传统嵌套布局层次结构会对性能产生负面影响

结果布局源应如下所示:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorDarkGray">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:alpha="0.7"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.03"
        android:text="Another text"
        android:textColor="@color/colorWhite"
        android:textSize="11sp"
        android:textStyle="normal"
        android:id="@+id/textView"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/textView2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:gravity="end"
        android:fontFamily="sans-serif"
        android:letterSpacing="0.03"
        android:text="Some text"
        android:textColor="@color/colorWhite"
        android:textSize="11sp"
        android:textStyle="normal"
        android:id="@+id/textView2"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>


您可以按照中的步骤学习如何使用ConstraintLayouts构建布局。

只需将RelativeLayout width设置为与父项匹配(0dp)。这会导致您将textView与父项左右对齐,并且RelativeLayout为wrap\u content ConstraintLayout未获得匹配\u父项。您必须将宽度设置为“0dp”,这感觉像是违背了约束布局的目的。为此,android:layout\u width=“0dp”和android:layout\u height=“wrap\u content”(在TextView上)解决了我的问题,即约束视图中的TextView忽略了边距而扩展以填充宽度。你知道记录在哪里吗?@Ewan也许记录在这里