2016-11-16 153 views
4

我的约束布局的版本是1.0.0-alpha8。之后,我已经包括在我的布局工具栏,则在工具栏的左边和右边两个空间,像下面Android约束布局奇怪行为

enter image description here

这里将图像的代码,我的工具栏

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

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

另一个STANGE的事情是,如果我编译或构建程序我的代码自动改变,像

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

目标SDK和哪个主题正在使用? –

+0

@AnkitaShah targetSdkVersion 24 and theme is Theme.AppCompat.Light.NoActionBar –

+0

试试这个 'Toolbar parent =(Toolbar)customView.getParent(); parent.setPadding(0,0,0,0); //对于标签,否则给标签空间 parent.setContentInsetsAbsolute(0,0);' 或者检查这个 http://stackoverflow.com/a/28086802/6005977 –

回答

13

首先,你应该更新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"/> 

请注意,您可以通过轻松创建的那些属性单击鼠标右键,并选择image

在Android Studio 2.2中,您需要按住键才能创建约束,在Android Studio 2.3中,默认情况下会创建约束。