Android约束布局奇怪的行为 [英] Android constraint layout strange behavior

查看:75
本文介绍了Android约束布局奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的约束布局的版本是1.0.0-alpha8.在布局中包含工具栏后,工具栏的左侧和右侧都有空间,如下面的图片

The version of my constraint layout is 1.0.0-alpha8 . After I have included a toolbar in my layout , there is space in both left and right side of the toolbar, like the image below

这是我的工具栏的代码

<?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>

我已按照以下方式包含在布局中

Which I have included in my layout in following way

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

我在布局文件的根元素中未使用任何其他填充或边距.

I didn't used any additional padding or margin in my root element of the layout file .

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

Another stange thing is if I compile or build the program my code automatically changed, like

<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自动添加.

And the guideline also add some additional value which I didn't write, like layout_editor_absoluteX automatically added .

<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"/>

推荐答案

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

First, you should update to ConstraintLayout beta 4.

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

Now, the root problem you have is that you are using match_parent on the toolbar -- this isn't supported by ConstraintLayout. You need to add:

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

,并使用0dp代替match_parent:

and use 0dp instead of 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"/>

请注意,通过右键单击组件并选择

Note that you can create those attributes easily by doing right-click on the component, and picking

在Android Studio 2.2上,您需要按住创建约束的键,在Android Studio 2.3上,默认情况下会创建约束.

On Android Studio 2.2 you need to hold the key to create the constraints, on Android Studio 2.3 it creates the constraints by default.

这篇关于Android约束布局奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆