【问题标题】:How to put text on right centre of image view on constraint layout如何在约束布局上将文本放在图像视图的右中心
【发布时间】:2018-03-21 20:55:49
【问题描述】:

以下是我整个 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 的属性? 谢谢

【问题讨论】:

  • 请添加此应用:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"
  • 是否要在图片中心和图片末端之间设置文字?
  • @MathaN 不是在中间,在右边,空间很小

标签: android xml android-layout android-constraintlayout


【解决方案1】:

请在你的文本视图中添加一行

app:layout_constraintBottom_toBottomOf="@+id/img_apn_not_set"

同时删除 android:layout_marginTop="5dp"

希望对你有帮助

【讨论】:

    【解决方案2】:

    试试这个,如果要将文本向右或向左移动,请使用水平偏移,如果要将文本移动到顶部或底部,请使用垂直偏移。

    <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" />
    

    【讨论】:

      【解决方案3】:

      试试这个

      <?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>
      

      结果

      【讨论】:

        【解决方案4】:
        <?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>
        

        【讨论】:

        • 非常感谢 :)
        • 很高兴为您提供帮助。
        【解决方案5】:

        您可以使用 app:layout_constraintWidth_default="wrap" 并将所有 4 个边约束设置为中心

        例子:

        <?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>
        

        看起来像这样

        【讨论】:

        • 谢谢,不理解这个“给所有 4 个侧面容器”你的意思是所有四个侧面的图像视图。 ?
        • 对不起,约束Top_toTopOfBottom_toBottomOf,左右也是
        猜你喜欢
        • 2021-12-16
        • 2021-11-24
        • 2020-03-22
        • 2019-05-04
        • 2021-12-26
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 2020-06-09
        相关资源
        最近更新 更多