如何以不同高度的约束布局中心2个元素

发布于 2025-02-05 20:30:38 字数 2422 浏览 1 评论 0原文

我试图以约束布局为中心元素,我可以使用指导行进行居中,然后将图像和文本视图放在其下方和下方,但是图像和文本视图的高度不同,因此不会将其集中。

任何最佳方法都请

         <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/noDataMessageLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="@dimen/margin_horizontal_small"
                    android:layout_marginBottom="@dimen/margin_vertical_small"
                    android:visibility="visible"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/monthSpinner"
                    app:layout_constraintBottom_toTopOf="@+id/monthlyChargeConsumptionLayout">

                    <ImageView
                        android:id="@+id/noDataImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_no_data"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"/>

                    <TextView
                        android:id="@+id/noDataMessageTV"
                        style="@style/Text.Small.Light"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginVertical="@dimen/margin_vertical_small"
                        android:gravity="center_vertical"
                        android:text="No charge session data\navailable for this month."
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/noDataImage" />
                </androidx.constraintlayout.widget.ConstraintLayout>

目前看起来。

感谢您提前的帮助 r

I am trying to centre elements in a constraint layout, I could use a guide line to be centred and then place the image and textview up and below it, but image and textview are of different height so they would not be centred.

any best approach on this please

         <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/noDataMessageLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="@dimen/margin_horizontal_small"
                    android:layout_marginBottom="@dimen/margin_vertical_small"
                    android:visibility="visible"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/monthSpinner"
                    app:layout_constraintBottom_toTopOf="@+id/monthlyChargeConsumptionLayout">

                    <ImageView
                        android:id="@+id/noDataImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_no_data"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"/>

                    <TextView
                        android:id="@+id/noDataMessageTV"
                        style="@style/Text.Small.Light"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginVertical="@dimen/margin_vertical_small"
                        android:gravity="center_vertical"
                        android:text="No charge session data\navailable for this month."
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/noDataImage" />
                </androidx.constraintlayout.widget.ConstraintLayout>

This is how it looks at the moment.

Thanks for your help in advance
R

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯然 2025-02-12 20:30:38

只需尝试对ImageView和TextView应用相同的约束

<androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/noDataMessageLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="@dimen/margin_horizontal_small"
                    android:layout_marginBottom="@dimen/margin_vertical_small"
                    android:visibility="visible"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/monthSpinner"
                    app:layout_constraintBottom_toTopOf="@+id/monthlyChargeConsumptionLayout">

                    <ImageView
                        android:id="@+id/noDataImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_no_data"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"/>

                    <TextView
                        android:id="@+id/noDataMessageTV"
                        style="@style/Text.Small.Light"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                android:layout_marginVertical="@dimen/margin_vertical_small"
                        android:gravity="center_vertical"
                        android:text="No charge session data\navailable for this month."
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>

Just try applying same constraints for Both ImageView and TextView

<androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/noDataMessageLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="@dimen/margin_horizontal_small"
                    android:layout_marginBottom="@dimen/margin_vertical_small"
                    android:visibility="visible"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/monthSpinner"
                    app:layout_constraintBottom_toTopOf="@+id/monthlyChargeConsumptionLayout">

                    <ImageView
                        android:id="@+id/noDataImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_no_data"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent"/>

                    <TextView
                        android:id="@+id/noDataMessageTV"
                        style="@style/Text.Small.Light"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                android:layout_marginVertical="@dimen/margin_vertical_small"
                        android:gravity="center_vertical"
                        android:text="No charge session data\navailable for this month."
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintBottom_toBottomOf="parent" />
                </androidx.constraintlayout.widget.ConstraintLayout>
深府石板幽径 2025-02-12 20:30:38

这将通过在约束点中使用链条来解决

app:layout_constraintHorizontal_chainStyle="packed"

链由多个视图组成,因此要创建一个链条,我们必须选择我们希望链条的视图,然后选择“水平中心”以创建水平链,或“垂直中心”以创建垂直链。

示例

来源:检查此以获取更多信息

This would be solved by using Chains in the ConstraintLayout

app:layout_constraintHorizontal_chainStyle="packed"

a chain consists of multiple views, so to create a chain we must select the views we wish to chain together, and then select either ‘Center Horizontally’ to create a horizontal chain, or ‘Center Vertically’ to create a vertical chain.

example

Create chains

Source : check this for more info

洛阳烟雨空心柳 2025-02-12 20:30:38

在垂直的中间,无论视图的相对高度如何,您都可以将它们放入具有包装的链式的垂直链中,如下所示:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/view1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toTopOf="@+id/view2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <View
        android:id="@+id/view2"
        android:layout_width="100dp"
        android:layout_height="300dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view1" />

</androidx.constraintlayout.widget.ConstraintLayout>

您也可以将它们定位为通过更改垂直偏置在垂直的任何地方。

To vertically center two views regardless of the relative heights of the views you can place them into a vertical chain with a chain style of packed as follows:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/view1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toTopOf="@+id/view2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <View
        android:id="@+id/view2"
        android:layout_width="100dp"
        android:layout_height="300dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view1" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

You can also position them anywhere vertically by changing the vertical bias.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文