如何将自定义布局的子级放置到此布局的特定区域中?

问题描述

重点是我希望自定义布局的子级位于其特定区域内。请看图片。这是我的自定义按钮,实际上是ConstraintLayout。我希望它的所有子项认都位于黑色区域。例如,我添加一个TextView作为此按钮布局的子元素,它会自动放置在黑色区域的内部,如果我想设置子元素的layout_constraintStart或类似的东西,它将为我带来最多的好处黑色区域的左点,而不是灰色区域的左点。

那么,有可能吗?我假设我需要更改一些属性,例如layoutBorders或重写一个方法。同样,我可以创建一个黑色区域大小的布局,并以某种方式将所有按钮的新子代绑定到黑色区域的布局。也许整个事情应该做不同的事情?

如果您对此感兴趣,请查看此组件的XML:

<?xml version="1.0" encoding="utf-8"?>
<merge 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"
    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/grey_zone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/coloredZoneEndGuideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginVertical="10dp"
            android:adjustViewBounds="true"
            android:src="@drawable/picture"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/rightGuideline"
            app:layout_constraintStart_toEndOf="@+id/leftGuideline"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/leftGuideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.33" />

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/rightGuideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.67" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <!-- Should not be here thus shortened -->
    <com.google.android.material.textview.MaterialTextView />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/coloredZoneEndGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.33" />

</merge>

1

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)