Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在约束布局1.1中使用新功能?_Android_Android Constraintlayout - Fatal编程技术网

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

Android 如何在约束布局1.1中使用新功能?,android,android-constraintlayout,Android,Android Constraintlayout,是否有人知道如何使用约束布局1.1中的新功能,即障碍和基于百分比的尺寸?网上绝对没有可用的文档,最近关于设计器工具的Google I/O讨论只详细介绍了占位符。顺便说一句,我发现了如何使用组,这也是一项新功能。您只需添加 <android.support.constraint.Group app:constraint_referenced_ids="button1, button2" android:layout_width="wrap_content" andro

是否有人知道如何使用约束布局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"/>

对于约束布局,其中app:constraint\u referenced\u id是一个字符串,您应该在其中枚举要与此组关联的视图的逗号分隔ID。现在,切换组的可见性将更改其引用的所有视图的可见性,我认为这是此功能目前的主要用途。

更新:终于到了


当第一次被问到这个问题时,新功能的数量非常少。我能找到的最好的是在!但是那里的信息给了我足够的提示来创建一个带有水平屏障的约束布局。它实际上起了作用,新的(beta版)约束布局也修复了
wrap\u content
的一些坏问题。我对Constraint Layout Beta版的第一印象非常积极,经过了大量的额外测试

在使用新内容之前,将
ConstraintLayout 1.1.0
添加到项目中

在app/build.gradle中,将约束布局依赖项更改为:

implementation'com.android.support.constraint:constraint布局:1.1.0'

您可能还需要将maven存储库添加到项目的build.gradle中(这是项目根目录中的另一个文件)。查找allprojects存储库部分并添加以下内容:
maven{url'https://maven.google.com“}
所以整个部分应该是这样的:

allprojects {
     repositories {
         jcenter()
         maven { url 'https://maven.google.com' }
     }
}
现在来看看有趣的东西!下面的代码段创建了一个水平屏障,以便
底部文本视图
位于
包含的布局
多行文本视图
的下方

<android.support.constraint.Barrier
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/barrier1"
    app:barrierDirection="bottom"
    app:constraint_referenced_ids="included_layout, multiline_textview" />

<TextView
    android:id="@+id/bottom_textview"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/barrier1"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />


  • @Vyacheslav A的答案对新功能的功能也有一个很好的总结。

    有一些关于屏障的信息。

    1。百分比维度

    宽度为0dp(或匹配约束)的窗口小部件的默认行为是分散的(可通过layout_constraintWidth_default属性配置)。在ConstraintLayout 1.0.x中,我们可以选择将其更改为wrap,在1.1.x中,我们有一个新的值percent,它允许我们设置一个小部件以占用一定百分比的可用空间

        <!-- the widget will take 40% of the available space -->
        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.4"
    
    然后,text1和text2将接收可见性信息

    原创的

    带有

    的正式文档下面是一个完整的示例,它完美地解释了constraint layout 1.1中添加的所有新功能

    但对于还有一件事需要注意,如果您将视图作为组中的引用id提供,则其单独的可见性将不起作用,为此,您需要为其创建一个单独的组。 如果我错了,请纠正我。 下面是使用组的示例代码,您可以在这里看到view1的可见性将不起作用,因为它被分配给一个组

        <?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.support.constraint.Group
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible"
            app:constraint_referenced_ids="view1,view2" />
    
        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/black"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
        <View
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/darker_gray"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view1" />
    
        <View
            android:id="@+id/view3"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/holo_blue_light"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view2" />
    
        <View
            android:id="@+id/view4"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/holo_red_light"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view3" />
    
    </android.support.constraint.ConstraintLayout>
    
    
    
    现在为了克服这个问题,我们可以创建一个新组来处理view1的可见性,如下所示

    <android.support.constraint.Group
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           app:constraint_referenced_ids="view1" />
    
    
    

    这是我遇到的解决方法,如果在这种情况下,我们希望在不同情况下处理组的可见性以及单个视图的可见性。

    有一种快捷方式可用于为选定视图创建组。它将自动以应该包含的方式包含所有ID。你应该试试看。另外,对于百分比,还有3个新属性(只需键入“app:per”并观看Android Studio提示)。谢谢!我去看看。谢谢你的信息!我们真的需要这些行吗?例如,缺少了app:layout\u constraintLeft\u toLeftOf=“parent”
    app:layout\u constraintRight\u toRightOf=“parent”
    app:layou constraintTop\u toBottomOf=“@+id/multiline\u textview”
    你说得对,@Lemberg!我已经相应地更新了答案。一个指向潜在解决方案的链接总是受欢迎的,但是请在链接周围添加上下文,这样您的其他用户就会知道它是什么以及它为什么存在。始终引用重要链接中最相关的部分,以防无法访问目标站点或永久脱机。看一看
    group.setVisibility(View.GONE);
    
        <?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.support.constraint.Group
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="visible"
            app:constraint_referenced_ids="view1,view2" />
    
        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/black"
            android:visibility="gone"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
        <View
            android:id="@+id/view2"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/darker_gray"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view1" />
    
        <View
            android:id="@+id/view3"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/holo_blue_light"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view2" />
    
        <View
            android:id="@+id/view4"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="@android:color/holo_red_light"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view3" />
    
    </android.support.constraint.ConstraintLayout>
    
    <android.support.constraint.Group
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           app:constraint_referenced_ids="view1" />