java - 如何在约束布局中创建容器以便设置背景?

标签 java android xml android-constraintlayout

如何在不嵌套 View 组的情况下对约束布局中的 View 进行分组?

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

这就是我现在正在做的创建容器 mBottomSheet我可以在其中对 View 进行分组并设置背景,但我正在使用另一个 View 组 constraintlayout为此,不建议这样做,因为约束布局的点也具有平面 View 层次结构。

那么我怎样才能实现同样的事情,但不使用另一个 View 组作为容器呢?

编辑

例子:

enter image description here

最佳答案

您可以使用Gudeliens实现类似的东西。

例如,您可以获取一些 View (我们称之为 x )并放置在您的布局上,然后将所有想要的 View 放在 x 上方.

至于 Gudeliens,与他们一起,您可以将您的 x以任何您想要使用的方式使用 app:layout_constraintGuide_percent属性。

以这种布局为例:

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

这个布局在没有任何嵌套 View 的情况下看起来像这样: (我正在向您添加预览图像可以更好地理解指南)

enter image description here

上面的布局是一个示例,说明如何在不使用嵌套 View 组的情况下实现这一目标。

这个解决方案对你有用,但我也同意 user1506104。
可以有一个非常小的嵌套级别,如果你不过度嵌套,你的布局性能可以保持不变
  • 你可以选择我的解决方案,也可以有一些嵌套 View (同样,不要过度)
    我更喜欢使用我的解决方案,这样我就可以避免嵌套。
  • 请记住,我的解决方案可能比在创建布局时使用一些嵌套杠杆要长一些。
  • 关于java - 如何在约束布局中创建容器以便设置背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57837671/

    相关文章:

    xml - 如何在 XSD 中 self 引用

    java - Helloworld 数据库 - Android 与 sqlite 的连接

    java - Java QuadCurve.2D 绘制不需要的线

    android - 在android中设置html字符串的子字符串的对齐方式

    Android标签点击滚动到顶部

    android - 为什么不在 Android 上的 NestedScrollView 中显示 View

    java - SQL,JAVA查询根据主键检索所有列

    java - 转换按钮未显示

    android - 为什么我将 Razorpay 与 Flutter 结合使用会出现此错误 => Uncaught TypeError :window. getDeviceDetails is not a function and BAD_REQUEST_ERROR?

    iphone - 在 objective-c 中从多级xml获取子标记