将 ImageView 与 textView 中的文本对齐
Align ImageView to end to text in textView
我需要将 imageView 与 textView 的末尾对齐,两者都限制在复选框的末尾。
我的代码的问题是 imageView 总是出现在 Checkbox 的开头,而不是 textView 中的文本结束的地方。谁能帮忙
<androidx.constraintlayout.widget.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="0dp"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/tvMerchantTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin4"
android:layout_marginEnd="@dimen/margin8"
android:layout_marginStart="@dimen/margin16"
android:drawablePadding="@dimen/padding4"
android:ellipsize="middle"
android:maxWidth="@dimen/width200"
android:singleLine="true"
android:textSize="@dimen/textSize12"
app:layout_constraintBottom_toTopOf="@id/tvMerchantSubtitle"
app:layout_constraintEnd_toStartOf="@id/cbMerchantSelect"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toEndOf="@id/ivMerchantLogo"
app:layout_constraintTop_toTopOf="@id/ivMerchantLogo" />
<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/cbMerchantSelect"
android:layout_width="@dimen/width16"
android:layout_height="@dimen/height16"
android:layout_marginEnd="@dimen/margin16"
android:button="@drawable/ic_round_checkbox"
android:clickable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我正在使用 glide 将图像设置为 textView end drawable -
Glide.with(this)
.load(url)
.into(object : CustomTarget<Drawable?>(
DisplayUtils.dpToPx(this.context, R.dimen.width30),
DisplayUtils.dpToPx(this.context, R.dimen.height20)
) {
override fun onLoadCleared(@Nullable placeholder: Drawable?) {}
override fun onResourceReady(
resource: Drawable,
transition: com.bumptech.glide.request.transition.Transition<in Drawable?>?
) {
mBinding.inPaymentMethod.tvMerchantTitle.setCompoundDrawablesWithIntrinsicBounds(
null,
null,
resource,
null
)
}
})
将 app:layout_constraintStart_toEndOf="@+id/tvMerchantTitle"
添加到您的 ImageView
示例代码:
<androidx.constraintlayout.widget.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="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/tvMerchantTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:drawablePadding="4dp"
android:ellipsize="middle"
android:maxWidth="200dp"
android:singleLine="true"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/ivMerchantLogo"
app:layout_constraintBottom_toBottomOf="@+id/ivMerchantLogo"
android:text="@string/app_name"/>
<ImageView
android:id="@+id/ivMerchantLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/tvMerchantTitle"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/cbMerchantSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:clickable="false"
android:gravity="end"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/ivMerchantLogo"
app:layout_constraintBottom_toBottomOf="@+id/ivMerchantLogo" />
</androidx.constraintlayout.widget.ConstraintLayout>
以上代码的输出如下:
我需要将 imageView 与 textView 的末尾对齐,两者都限制在复选框的末尾。 我的代码的问题是 imageView 总是出现在 Checkbox 的开头,而不是 textView 中的文本结束的地方。谁能帮忙
<androidx.constraintlayout.widget.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="0dp"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/tvMerchantTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin4"
android:layout_marginEnd="@dimen/margin8"
android:layout_marginStart="@dimen/margin16"
android:drawablePadding="@dimen/padding4"
android:ellipsize="middle"
android:maxWidth="@dimen/width200"
android:singleLine="true"
android:textSize="@dimen/textSize12"
app:layout_constraintBottom_toTopOf="@id/tvMerchantSubtitle"
app:layout_constraintEnd_toStartOf="@id/cbMerchantSelect"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintStart_toEndOf="@id/ivMerchantLogo"
app:layout_constraintTop_toTopOf="@id/ivMerchantLogo" />
<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/cbMerchantSelect"
android:layout_width="@dimen/width16"
android:layout_height="@dimen/height16"
android:layout_marginEnd="@dimen/margin16"
android:button="@drawable/ic_round_checkbox"
android:clickable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我正在使用 glide 将图像设置为 textView end drawable -
Glide.with(this)
.load(url)
.into(object : CustomTarget<Drawable?>(
DisplayUtils.dpToPx(this.context, R.dimen.width30),
DisplayUtils.dpToPx(this.context, R.dimen.height20)
) {
override fun onLoadCleared(@Nullable placeholder: Drawable?) {}
override fun onResourceReady(
resource: Drawable,
transition: com.bumptech.glide.request.transition.Transition<in Drawable?>?
) {
mBinding.inPaymentMethod.tvMerchantTitle.setCompoundDrawablesWithIntrinsicBounds(
null,
null,
resource,
null
)
}
})
将 app:layout_constraintStart_toEndOf="@+id/tvMerchantTitle"
添加到您的 ImageView
示例代码:
<androidx.constraintlayout.widget.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="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/tvMerchantTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="16dp"
android:drawablePadding="4dp"
android:ellipsize="middle"
android:maxWidth="200dp"
android:singleLine="true"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/ivMerchantLogo"
app:layout_constraintBottom_toBottomOf="@+id/ivMerchantLogo"
android:text="@string/app_name"/>
<ImageView
android:id="@+id/ivMerchantLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@+id/tvMerchantTitle"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.appcompat.widget.AppCompatCheckBox
android:id="@+id/cbMerchantSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:clickable="false"
android:gravity="end"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/ivMerchantLogo"
app:layout_constraintBottom_toBottomOf="@+id/ivMerchantLogo" />
</androidx.constraintlayout.widget.ConstraintLayout>
以上代码的输出如下: