【问题标题】:Android constraint layout strange behaviorAndroid约束布局奇怪行为
【发布时间】:2016-11-16 13:23:35
【问题描述】:

我的约束布局版本是 1.0.0-alpha8 。在我的布局中包含一个工具栏后,工具栏的左右两侧都有空间,如下图所示

这是我的工具栏的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    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="?attr/actionBarSize"
    android:background="@color/colorPrimary">


</android.support.v7.widget.Toolbar>

我通过以下方式将其包含在我的布局中

<include
    layout="@layout/toolbar_top"
    android:id="@+id/topPanel"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

我没有在布局文件的根元素中使用任何额外的填充或边距。

另一个奇怪的事情是,如果我编译或构建程序,我的代码会自动更改,例如

<include
    layout="@layout/toolbar_top"
    android:id="@+id/topPanel"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"/>

改成

<include
    layout="@layout/toolbar_top"
    android:id="@+id/topPanel"
    android:layout_height="wrap_content"
    android:layout_width="368dp"/>

并且该指南还添加了一些我没有写的附加值,例如layout_editor_absoluteX自动添加。

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline1"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.15"
    tools:layout_editor_absoluteX="58dp"/>

【问题讨论】:

  • 目标 SDK & 使用哪个主题?
  • @AnkitaShah targetSdkVersion 24,主题是 Theme.AppCompat.Light.NoActionBar
  • 试试这个Toolbar parent =(Toolbar) customView.getParent(); parent.setPadding(0,0,0,0);//for tab otherwise give space in tab parent.setContentInsetsAbsolute(0,0);或检查这个stackoverflow.com/a/28086802/6005977
  • @AnkitaShah 我的问题是为什么它会在预览中显示这个并添加额外的代码

标签: android android-constraintlayout


【解决方案1】:

首先,您应该更新到 ConstraintLayout beta 4。

现在,您遇到的根本问题是您在工具栏上使用match_parent - ConstraintLayout 不支持。您需要添加:

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"

并使用 0dp 代替 match_parent:

<include
    layout="@layout/toolbar_top"
    android:id="@+id/topPanel"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

请注意,您可以通过右键单击组件并选择 轻松创建这些属性

在 Android Studio 2.2 上,您需要持有创建约束的密钥,在 Android Studio 2.3 上,它默认创建约束。

【讨论】:

    猜你喜欢
    • 2016-10-22
    • 1970-01-01
    • 2013-03-16
    • 2014-08-28
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多