如何以不同高度的约束布局中心2个元素
我试图以约束布局为中心元素,我可以使用指导行进行居中,然后将图像和文本视图放在其下方和下方,但是图像和文本视图的高度不同,因此不会将其集中。
任何最佳方法都请
<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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需尝试对ImageView和TextView应用相同的约束
Just try applying same constraints for Both ImageView and TextView
这将通过在约束点中使用链条来解决
示例
来源:检查此以获取更多信息
This would be solved by using Chains in the ConstraintLayout
example
Source : check this for more info
在垂直的中间,无论视图的相对高度如何,您都可以将它们放入具有
包装
的链式的垂直链中,如下所示:您也可以将它们定位为通过更改垂直偏置在垂直的任何地方。
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:You can also position them anywhere vertically by changing the vertical bias.