gpt4 book ai didi

java - 如何在约束布局中将 9X 大小相同的按钮放在 ImageView 上?

转载 作者:行者123 更新时间:2023-12-02 02:47:45 24 4
gpt4 key购买 nike

我在约束布局中有一个 ImageView,如下所示

<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/argentina"
android:layout_marginLeft="16dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.501"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />

我想用 9X 大小相同的按钮覆盖此图像,如下所示 Problem Describing Image

注意 Imageview 随着约束布局的大小而变化

最佳答案

是的,您可以使用constraint chainma​​tch_constraint(0dp)属性来实现这一点您必须在此处定义垂直 水平 到所有按钮,下面是相同的示例

<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"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">



<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/firefox"
android:layout_marginLeft="16dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.501"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/button5"
app:layout_constraintBottom_toTopOf="@id/button41"
app:layout_constraintVertical_chainStyle="spread_inside"/>


<Button
android:id="@+id/button31"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView"
app:layout_constraintBottom_toTopOf="@id/button311"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintEnd_toStartOf="@+id/button51"
app:layout_constraintVertical_chainStyle="spread_inside" />

<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintTop_toTopOf="@+id/imageView"
app:layout_constraintEnd_toStartOf="@+id/button11"
app:layout_constraintStart_toEndOf="@+id/button13"
app:layout_constraintBottom_toTopOf="@+id/button133"
app:layout_constraintVertical_chainStyle="spread_inside" />




<Button
android:id="@+id/button311"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/button31"
app:layout_constraintBottom_toTopOf="@+id/button13"
app:layout_constraintEnd_toStartOf="@+id/button133"
app:layout_constraintHorizontal_chainStyle="spread_inside" />

<Button
android:id="@+id/button133"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintStart_toEndOf="@+id/button311"
app:layout_constraintEnd_toStartOf="@+id/button41"
app:layout_constraintTop_toBottomOf="@+id/button5"
app:layout_constraintBottom_toTopOf="@+id/button51" />
<Button
android:id="@+id/button41"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="@+id/imageView"
android:text="Button"
app:layout_constraintTop_toBottomOf="@+id/button1"
app:layout_constraintBottom_toTopOf="@+id/button11"
app:layout_constraintStart_toEndOf="@+id/button133" />

<Button
android:id="@+id/button11"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintStart_toEndOf="@+id/button51"
app:layout_constraintTop_toBottomOf="@id/button41"
/>


<Button
android:id="@+id/button13"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@id/button311"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintEnd_toStartOf="@+id/button51" />

<Button
android:id="@+id/button51"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toStartOf="@+id/button11"
app:layout_constraintStart_toEndOf="@+id/button13"
app:layout_constraintTop_toBottomOf="@id/button133"/>
</android.support.constraint.ConstraintLayout>

这是设计蓝图 design and blue print

您可以在此处获取有关链和约束的所有详细信息 https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#Chains

这适用于所有设备尺寸和方向

注意:我建议您也向 ImageView 添加一些 minHeight,以避免隐藏按钮,但我不知道您的要求,但这是一个值得注意的点

关于java - 如何在约束布局中将 9X 大小相同的按钮放在 ImageView 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44339685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com