如何在 ConstraintLayout 的 ImageView 中将文本居中放置在右侧?

7
以下是我的 Constraint Layout 的部分 XML 代码。
<ImageView
            android:id="@+id/img_apn_not_set"
            style="@style/DeviceManagementImageView"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_sos"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view1" />

        <TextView
            android:id="@+id/tv_apn_not_set"
            style="@style/DeviceManagementHeaderText"
            android:text="Apn not set"
            android:layout_marginTop="5dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/img_apn_not_set"

            app:layout_constraintTop_toTopOf="@+id/img_apn_not_set" />

我想要做的是将文本视图放在图像视图右侧的正中间。在线性布局中,我们通常通过重力实现这一点。这里我使用marginTop来实现同样的效果。所以我是否可以使用任何属性来实现相同的效果呢?是否有类似于rightOfCentreOf的属性呢? 谢谢。

1
请添加此代码:app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set" - Saurabh Vadhva
你想把文本设置在图像中心和图像末端之间吗? - MathankumarK
@MathaN 不是在中心,而是在右侧留有一点空间。 - Pritish
5个回答

7
请在您的TextView中添加一行。
app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"

还要删除 android:layout_marginTop="5dp"

希望这可以帮助你


4

如果你想将文本向右或向左移动,可以使用水平偏差;如果你想将文本向上或向下移动,则需要使用垂直偏差。

<TextView
        android:id="@+id/tv_apn_not_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/app_name"
        app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"
        app:layout_constraintEnd_toEndOf="@+id/img_apn_not_set"
        app:layout_constraintHorizontal_bias="0.63"
        app:layout_constraintStart_toStartOf="@+id/img_apn_not_set"
        app:layout_constraintTop_toTopOf="@+id/img_apn_not_set" />

2

试试这个

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/AmountLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBaseline_toBaselineOf="@id/Amount"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/Amount"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:text="NILESH"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@id/AmountLabel"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

结果

这里输入图片描述


2
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img_apn_not_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_apn_not_set"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Apn not set"
        app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/img_apn_not_set"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

非常感谢您的帮助! - Pritish
很高兴能帮助你。 - Zezariya Nilesh

1
你可以使用 app:layout_constraintWidth_default="wrap" ,并给所有四个边界添加约束,这样它就会居中。
例如:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img_apn_not_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher_foreground"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/tv_apn_not_set"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Apn not set"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintTop_toTopOf="@+id/img_apn_not_set"
        app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"
        app:layout_constraintLeft_toRightOf="@+id/img_apn_not_set"
        app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>

它将会是这个样子。

enter image description here


谢谢,我不明白“给所有四边容器”的意思,您是指图像视图的所有四边吗? - Pritish
抱歉,这是像“Top_toTopOf”、“Bottom_toBottomOf”、右侧和左侧等约束。 - Muthukrishnan Rajendran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接