Android constraint layout strange behavior

爷,独闯天下 提交于 2019-12-10 15:53:47

问题


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

Here is the code for my toolbar

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

Change to

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

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

回答1:


First, you should update to ConstraintLayout beta 4.

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"

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

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.



来源:https://stackoverflow.com/questions/40633299/android-constraint-layout-strange-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!