Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.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
Java 如何在constraintlayout中创建容器以便设置背景?_Java_Android_Xml_Android Constraintlayout - Fatal编程技术网

Java 如何在constraintlayout中创建容器以便设置背景?

Java 如何在constraintlayout中创建容器以便设置背景?,java,android,xml,android-constraintlayout,Java,Android,Xml,Android Constraintlayout,如何在不嵌套视图组的情况下对约束布局中的视图进行编组 <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:lay

如何在不嵌套视图组的情况下对约束布局中的视图进行编组

<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"
  android:orientation="vertical"
  android:background="@color/color1">

<ImageView
    android:id="@+id/mAlbumArtLarge"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:scaleType="fitXY"
    android:src="@drawable/image1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="ContentDescription" />

<android.support.constraint.ConstraintLayout
    android:id="@+id/mBottomSheet"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="#20000000"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent">

        <ImageView
         android:id="@+id/mAlbumIvBottom"
         android:layout_width="50dp"
         android:layout_height="50dp"
         android:layout_margin="5dp"
         android:scaleType="fitXY"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

您可以尝试: androidx.constraintlayout.widget.Group

您可以通过相对于彼此位置分配约束来将视图分组在一起,但您无法对所有小部件应用背景,因为它们仍然是父视图组的子视图。有许多其他选项可以避免使用嵌套ConstraintLayout

  • 可以在ConstraintLayout内部使用另一个视图组,如LinearLayout或RelativeLayout
  • 您可以为底部工作表创建一个单独的布局,并将其包括在内,以获得干净的代码
  • 你可以用它来实现类似的目标

    例如,您可以获取一些视图(我们称之为
    x
    )并将其放置在布局上,只需将所有需要的视图放在
    x
    上方即可

    对于Gudeliens,使用它们,您可以使用
    app:layout\u constraintGuide\u percent
    属性以任何您想要使用的方式放置
    x

    以这个布局为例:

    <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"
      android:background="@color/colorPrimary"
      android:orientation="vertical">
    
    <ImageView
        android:id="@+id/mAlbumIvBottom"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/ic_launcher_background"
        android:scaleType="fitXY"
        android:elevation="6dp"
        android:layout_margin="6dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline6"
        app:layout_constraintEnd_toStartOf="@+id/guideline7"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button2" />
    
    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="I am view x"
        app:layout_constraintBottom_toTopOf="@+id/guideline6"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    
    <android.support.constraint.Guideline
        android:id="@+id/guideline6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent=".15" />
    
    <android.support.constraint.Guideline
        android:id="@+id/guideline7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".3" />
    
    
    </android.support.constraint.ConstraintLayout>
    

    上面的布局示例说明了如何在不使用嵌套视图组的情况下实现这一点


    此解决方案适用于您,但我也同意user1506104。 有一个非常小的嵌套级别是可以的,如果不过度嵌套,布局性能可以保持不变

    • 您可以使用我的解决方案,也可以使用一些嵌套视图(同样,不要过度使用) 我更喜欢使用我的解决方案,这样可以避免嵌套

    • 请记住,在创建布局时,我的解决方案可能比只进行某种嵌套要长一点


    但是在constraintLayout中使用LinearLayout或RelativeLayout作为子视图会违反constraintLayout的平面视图层次结构,在constraintLayout中嵌套视图组不被视为性能差吗?这就是视图组的用途,不,它不会影响性能,因为底部工作表是一个单独的组。如果您不在底部工作表中使用subgroup,那么底部工作表的小部件将是父约束布局的直接子部件和其他小部件的同级。这不是一个好的层次结构。建议使用,但不是必需的。请确保嵌套不要太深。@user1506104,以便在constraintlayout中有1-2个childview视图组(线性/相对)时不会对性能造成太大影响?是的。如果有,您会立即识别。@user1506104好的,谢谢您提供的信息,看起来我很好,因为我没有注意到任何延迟。谢谢您的回答,但我想要实现的是一个可以设置背景颜色的容器。例如,带有白色背景的线性布局中的视图就像这些视图的容器,我想实现同样的效果,但不需要线性布局。这不正是我所做的吗?如果按钮的id为
    button2
    ,则可以更改其背景颜色,甚至更改视图的类型。这和你描述的一模一样。我编辑我的帖子是为了澄清我的意思,因为我不确定你的意思。正如您在我的示例中所看到的,我使用了一个带有黑色背景的linearlayout作为容器,如何在不使用该linearlayout的情况下避免嵌套。哦,好的,我可以解释的最好方式是:我使用了我的按钮,而不是您的
    linearlayout
    容器(这导致了无嵌套情况)。现在,在我按下按钮之后,我所做的就是将我的其他视图约束到它。其他视图不在按钮内部-它们仅限于按钮及其上方(因此您可以看到它们)。您也可以选择任何方式更改按钮,从颜色、大小、位置等开始。如果有任何不可理解的地方,请随时询问