标签: android-constraintlayout

如何在约束布局1.1中使用新功能?

有谁知道如何在约束布局1.1中使用新功能,即障碍和基于百分比的维度?绝对没有在线提供的文档,最近关于设计器工具的Google I/O讨论仅详细介绍了占位符.顺便说一句,我发现了如何使用组,这也是一个新功能.你需要简单地添加

<android.support.constraint.Group
    app:constraint_referenced_ids="button1, button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

到您的约束布局,其中app:constraint_referenced_ids是一个字符串,您应该枚举要与该组关联的逗号分隔的视图ID.现在,切换组的可见性会更改其引用的所有视图的可见性,我认为这是此功能的主要目的.

android android-constraintlayout

20
推荐指数
2
解决办法
1万
查看次数

Android Studio 3 - Constraint布局编辑器坏了

我在macOS上使用Android Studio 3.0(今天更新到canary 4)用于侧面项目,并且最近(不确定何时)约束布局编辑器停止正常工作.

现在它只显示一个灰色窗口,蓝图视图根本不起作用.即使右侧的属性编辑器也不再显示约束.

这里只用一个按钮就可以看到简单的布局:

Android约束布局编辑器坏了

我正在使用约束布局,1.0.2但它以相同的方式失败1.1.0-beta1.

什么可能出错?IDE或idea.log中未显示任何错误

提前感谢您提供的任何帮助:)

android-constraintlayout android-studio-3.0

20
推荐指数
1
解决办法
7582
查看次数

如何在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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.plarke.proto1.QuizActivity">

<Button
    android:id="@+id/compare_settings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="16dp"
    android:layout_marginRight="16dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginEnd="16dp" />

<TextView
    android:id="@+id/compare_score"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginStart="16dp" />

<SeekBar
    android:id="@+id/compare_pitchbar"
    android:layout_width="0dp"
    android:layout_height="23dp"
    android:layout_marginBottom="50dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:progressBackgroundTint="@color/colorAccent"
    android:progressBackgroundTintMode="src_over"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="50dp"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    tools:text="The seekbar is all the way to the left."
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent" …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-constraintlayout

20
推荐指数
1
解决办法
1万
查看次数

如何在ConstraintLayout中创建可访问的焦点组?

想象一下,你有一个LinearLayout里面RelativeLayout包含3个TextViews具有artist, song and album:

<RelativeLayout
    ...
    <LinearLayout
        android:id="@id/text_view_container"
        android:layout_width="warp_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@id/artist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Artist"/>

        <TextView
            android:id="@id/song"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Song"/>

        <TextView
            android:id="@id/album"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="album"/>
    </LinearLayout>

    <TextView
        android:id="@id/unrelated_textview1/>
    <TextView
        android:id="@id/unrelated_textview2/>
    ...
</RelativeLayout>        
Run Code Online (Sandbox Code Playgroud)

当您激活TalkbackReader并单击其中的a TextViewLinearLayout,TalkbackReader将显示"艺术家","歌曲"或"专辑".

但是您可以TextViews通过使用以下方法将前3个放入焦点组:

<LinearLayout
    android:focusable="true
    ...
Run Code Online (Sandbox Code Playgroud)

现在,TalkbackReader将阅读"艺术家歌曲专辑".

2 unrelated TextViews仍然是他们自己而不是阅读,这是我想要实现的行为.

(请参阅Google codelabs示例以供参考)

我现在正试图重新创建这种行为,ConstrainLayout但没有看到如何.

<ConstraintLayout>
    <TextView artist/>
    <TextView song/>
    <TextView album/>
    <TextView unrelated_textview1/>
    <TextView unrelated_textview2/>
</ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

将小部件放入"组"似乎不起作用:

<android.support.constraint.Group
    android:id="@+id/group"
    android:layout_width="wrap_content" …
Run Code Online (Sandbox Code Playgroud)

android accessibility talkback android-constraintlayout

20
推荐指数
2
解决办法
1965
查看次数

ConstraintLayout无法在蓝图模式之外正确呈现

我今天开始使用Android Studio 2.2和ConstraintLayout,但我在渲染方面遇到了一些问题.使用蓝图模式时,一切正常.使用"设计模式"时,对象会粘在左上角,并且全部拼接在一起.

Android Studio警告:onMeasure错误

java.lang.NoSuchMethodError: android.support.constraint.solver.widgets.ConstraintWidget.immediateConnect(Landroid/support/constraint/solver/widgets/ConstraintAnchor$Type;Landroid/support/constraint/solver/widgets/ConstraintWidget;Landroid/support/constraint/solver/widgets/ConstraintAnchor$Type;II)V
    at android.support.constraint.ConstraintLayout.setChildrenConstraints(ConstraintLayout.java:517)
    at android.support.constraint.ConstraintLayout.updateHierarchy(ConstraintLayout.java:433)
    at android.support.constraint.ConstraintLayout.onMeasure_Original(ConstraintLayout.java:728)
    at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java)
    at android.view.View.measure(View.java:19731)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:19731)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
    at android.view.View.measure(View.java:19731)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:545)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:342)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:548)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:533)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$72(RenderTask.java:659)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Run Code Online (Sandbox Code Playgroud)

依赖:

compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha8'
Run Code Online (Sandbox Code Playgroud)

这是Android Studio中无法解决的错误还是有可解决的问题?SDK已更新.

编辑

XML文件:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:src="@drawable/about"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView6"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        app:layout_constraintLeft_toLeftOf="parent" …
Run Code Online (Sandbox Code Playgroud)

android-studio android-constraintlayout

19
推荐指数
1
解决办法
1万
查看次数

在约束布局中切换链组的可见性

在以前的xml布局中,我有多个视图组,其中包含很少的元素.隐藏每个视图组也会隐藏其所有子元素.因为我想要平面结构并尝试过ConstraintLayout.酷我知道如何链接元素与蔓延正确对齐.由于扁平结构没有包裹LinearLayout,现在我有3个视图隐藏.我想知道是否有替代方案来实现这一目标.

没有约束布局

<RelativeLayout....
..........
..........
<LinearLayout
        android:visibility="gone"
        tools:visibility="visible"
        android:id="@+id/filter_area"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/lblTerminal"
            android:background="@color/lightGray"
            style="@style/PurpleSubtitle"
            android:drawableRight="@drawable/i_down_yellow"
            android:drawableEnd="@drawable/i_down_yellow"
            android:padding="10dp"
            android:text="@string/lblTerminal"
            android:layout_weight="5"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

        <View
            android:background="@android:color/black"
            android:layout_width="1dp"
            android:layout_height="match_parent"/>

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/lblCategory"
            android:background="@color/lightGray"
            android:padding="10dp"
            android:drawableRight="@drawable/i_down_yellow"
            android:drawableEnd="@drawable/i_down_yellow"
            style="@style/PurpleSubtitle"
            android:text="@string/lblCategory"
            android:layout_weight="5"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />


    </LinearLayout>
  .......
  .......
  </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

约束布局

    <android.support.constraint.ConstraintLayout
    .....
    .....
    .....
       #happy that i no longer need LinearLayout for align properly
       <android.support.v7.widget.AppCompatTextView
            android:id="@+id/lblTerminal"
            android:background="@color/lightGray"
            style="@style/PurpleSubtitle"
            android:drawableRight="@drawable/i_down_yellow"
            android:drawableEnd="@drawable/i_down_yellow"
            android:padding="10dp"
            android:text="@string/lblTerminal"
            android:layout_weight="5"
            android:layout_width="0dp"
            android:layout_height="50dp"
            app:layout_constraintTop_toBottomOf="@+id/txt_search"
            app:layout_constraintRight_toLeftOf="@+id/view3"
            app:layout_constraintLeft_toLeftOf="@+id/guideline2"
            app:layout_constraintHorizontal_chainStyle="spread"/>

        <View
            android:background="@android:color/black"
            android:layout_width="1dp" …
Run Code Online (Sandbox Code Playgroud)

xml android visibility android-layout android-constraintlayout

19
推荐指数
2
解决办法
1万
查看次数

在通知中使用ConstraintLayout时崩溃

InflationException当我ConstraintLayout在自定义中使用a时,我得到了一个Notification.我可以在我的应用程序的其他地方使用它们,而不是在自定义布局中Notification.我假设支持库中包含的任何小部件根本无法在a中使用RemoteView,但我找不到任何确认.有人知道吗?

E/StatusBar: couldn't inflate view for notification com.example.app/0x1
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.constraint.ConstraintLayout" on path: DexPathList[[],nativeLibraryDirectories=[/system/priv-app/TeleService/lib/arm64, /system/fake-libs64, /system/priv-app/TeleService/TeleService.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.widget.RemoteViews.inflateView(RemoteViews.java:3278)
    at android.widget.RemoteViews.-wrap1(Unknown …
Run Code Online (Sandbox Code Playgroud)

java android android-notifications android-constraintlayout

19
推荐指数
2
解决办法
3110
查看次数

ConstraintLayout中的android RecylerView不会滚动

我在约束布局中有一个recyclerView,我无法使其滚动,列表只是在屏幕下方继续,没有滚动可能性.如果我将布局转换为相对布局,滚动工作正常.

怎么才能让它滚动?

以下XML显示了我的布局,Recycler视图位于底部.布局在屏幕顶部有一个图像和描述.此屏幕设置占用屏幕的30%.然后是一个分隔符和一个应该占据屏幕其余部分并且无法滚动的回收器视图

<?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="match_parent"
    android:background="@color/gradient_top">

   <android.support.v7.widget.AppCompatImageView
        android:id="@+id/imageViewLock"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="16dp"
        app:layout_constraintBottom_toTopOf="@+id/textViewPermissionsTitle"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_phone_lock"/>

    <TextView
        android:id="@+id/textViewPermissionsTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:gravity="center"
        android:paddingLeft="24dp"
        android:paddingRight="24dp"
        android:text="@string/allow_permissions"
        android:textColor="@color/white"
        android:textSize="@dimen/description_text_size"
        app:layout_constraintBottom_toTopOf="@+id/guideline1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.3"/>


    <View
        android:id="@+id/viewSeparator"
        android:layout_width="match_parent"
        android:layout_height="0.7dp"
        android:layout_marginTop="10dp"
        android:background="@color/bright_blue"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline1"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewPermissions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbarSize="1dp"
        android:scrollbarThumbVertical="@color/white"
        android:scrollbars="vertical"
        app:layout_constraintTop_toBottomOf="@+id/viewSeparator" />


</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

android android-scrollview android-recyclerview android-constraintlayout

19
推荐指数
2
解决办法
9735
查看次数

如何在ConstraintLayout中对多个视图进行分组

我在ConstraintLayout中添加了3个按钮.我添加了一个按钮来禁用或启用这些按钮.

如果我使用普通的LinearLayout.我可以将所有按钮放在线性布局中,并启用或禁用该特定布局.

但我正在使用ConstraintLayout.所以我需要禁用或启用所有这些按钮,我相信ConstraintLayout必须有一种方法来组合不同的视图.

请指导我如何在ConstriantLayout中对视图进行分组

在此输入图像描述

  <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="16dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="16dp" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        android:layout_marginStart="8dp"
        app:layout_constraintLeft_toRightOf="@+id/button"
        android:layout_marginLeft="8dp"
        app:layout_constraintTop_toTopOf="@+id/button" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        app:layout_constraintTop_toTopOf="@+id/button2"
        android:layout_marginEnd="16dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="16dp"
        android:layout_marginStart="8dp"
        app:layout_constraintLeft_toRightOf="@+id/button2"
        android:layout_marginLeft="8dp" />
Run Code Online (Sandbox Code Playgroud)

performance android android-layout android-linearlayout android-constraintlayout

18
推荐指数
1
解决办法
1万
查看次数

ConstraintLayout:设置行中所有视图的高度以匹配最高的视图

我正在尝试使用ConstraintLayout(版本1.0.2)来设置2个并排视图的高度以匹配其中最高的一个.这用作RecyclerView的ViewHolder,其中每个TextView获取任意长度的文本......

如果我将每个设置为wrap_content,则较短的一个将缩小.如果我将两者都设置为0dp(match_contraints),则两者都以0高度结束.

这是设置:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/id1"
        android:layout_width="60dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/id2"/>

    <TextView
        android:id="@+id/id2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/id1"
        app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

我想这是一个错误,因为"0dp"应该更像match_parent而不是实际的0 dp.

顺便说一下,在iOS上,等效的自动布局规则(设置视图的高度以匹配父级的顶部和底部)产生预期的结果.

当然使用LinearLayout非常简单,但是这个布局在更大的布局中起作用,我想修剪视图层次结构......

android android-constraintlayout

18
推荐指数
1
解决办法
6562
查看次数