我試圖在約束布局中居中元素,我可以使用引導線居中,然后將影像和 textview 放置在其上方和下方,但 image 和 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_toBottomOf="@ id/noDataImage" />
</androidx.constraintlayout.widget.ConstraintLayout>
這就是它目前的樣子。
提前感謝您的幫助 R

uj5u.com熱心網友回復:
只需嘗試對 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>
uj5u.com熱心網友回復:
這可以通過在 ConstraintLayout 中使用 Chains 來解決
app:layout_constraintHorizontal_chainStyle="packed"
一個鏈由多個視圖組成,因此要創建一個鏈,我們必須選擇我們希望鏈接在一起的視圖,然后選擇“水平居中”來創建水平鏈,或者選擇“垂直居中”來創建垂直鏈。
例子

來源:
您還可以通過更改垂直偏差將它們垂直放置在任何位置。

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/489240.html
上一篇:撒克遜:可分配的折舊時間線
