如何在不嵌套 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 组作为容器呢?
编辑
例子:
最佳答案
您可以使用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 的情况下看起来像这样: (我正在向您添加预览图像可以更好地理解指南)
上面的布局是一个示例,说明如何在不使用嵌套 View 组的情况下实现这一目标。
这个解决方案对你有用,但我也同意 user1506104。
可以有一个非常小的嵌套级别,如果你不过度嵌套,你的布局性能可以保持不变
我更喜欢使用我的解决方案,这样我就可以避免嵌套。
关于java - 如何在约束布局中创建容器以便设置背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57837671/