【问题标题】:Android - Image Right of Text Overlaps Screen HorizontallyAndroid - 文本的图像右侧与​​屏幕水平重叠
【发布时间】:2021-05-25 16:12:33
【问题描述】:

我正在尝试在 TextView 的右侧使用单行图像。 当文本很短时,它可以正常工作。问题是文本太大并被截断。文字填满LinearLayout,图片不在屏幕上

短文:

长文:

代码:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="32sp"
            android:ellipsize="end"
            android:maxLines="1"
            tools:text="Lorem ipsum dolor sit amet, consectetur"/>

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/ivInfectedDevOtherInfoIcon"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginStart="8dp"
            app:layout_constraintTop_toTopOf="@+id/ivInfectedDevOtherName"
            app:layout_constraintStart_toEndOf="@+id/ivInfectedDevOtherName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0"
            app:srcCompat="@drawable/ic_tv" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

当文本很长时,如何使图像停靠在右侧以使其不重叠?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    只需对您的 xml 布局进行以下更改:

    1.在LinearLayout中添加:

    android:layout_width="wrap_content"
    app:layout_constraintHorizontal_bias="0"
    

    2.在TextView中添加:

    android:layout_weight="1"
    

    3.在AppCompatImageView中添加:

    android:layout_weight="0"
    

    最终的 xml 布局如下:

    <?xml version="1.0" encoding="utf-8"?>
    <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="match_parent">
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHorizontal_bias="0">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="32sp"
                android:ellipsize="end"
                android:layout_weight="1"
                android:maxLines="1"
                tools:text="Lorem ipsum dolor sit amet, consectetur "/>
    
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/ivInfectedDevOtherInfoIcon"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginStart="8dp"
                android:layout_weight="0"
                app:layout_constraintTop_toTopOf="@+id/ivInfectedDevOtherName"
                app:layout_constraintStart_toEndOf="@+id/ivInfectedDevOtherName"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0"
                app:srcCompat="@drawable/ic_tv" />
    
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    长文本结果:

    小文本的结果:

    【讨论】:

      【解决方案2】:

      编辑 1:来自 cmets:

      这适用于文本较长且被截断的情况。当文本是 短图像停留在右侧并且有更大的间隙 然后是两个元素之间的预期 8dp

      因此,除非视图直接插入 TextView,否则您正在寻找的行为将不起作用,这将测量当前宽度并在到达 drawable 的末尾时截断而不是文本。所以你可以创建这样的组件或使用drawableEnddrawableEndCompat

      使用drawableEndCompat,您的布局将如下所示(您可以删除LinearLayout IMO):

      <?xml version="1.0" encoding="utf-8"?>
      <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="match_parent">
      
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent">
      
              <TextView
                  android:id="@+id/tvText"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:ellipsize="end"
                  android:maxLines="1"
                  android:textSize="32sp"
                  app:drawableEndCompat="@drawable/ic_add"
                  android:text="Lorem cd" />
      
          </LinearLayout>
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      如果您想点击drawableEndCompat,那么您可以像这样查看 SO 上的答案:Implement onClick only for a TextView compound drawable

      示例:

      @SuppressLint("ClickableViewAccessibility")
          private void setOnEndClickListener() {
              TextView txtview = (TextView) findViewById(R.id.tvText);
              txtview.setOnTouchListener(new View.OnTouchListener() {
                  @Override
                  public boolean onTouch(View v, MotionEvent event) {
                      if (event.getAction() == MotionEvent.ACTION_UP) {
                          if (event.getRawX() >= txtview.getRight() - txtview.getTotalPaddingRight()) {
                              Log.d("MG-onClick", "Drawable Clicked");
                              return true;
                          }
                      }
                      return true;
                  }
              });
          }
      

      旧答案:

      layout_weight=1layout_width="0dp" 添加到您的TextView

      所以它看起来像这样:

      <?xml version="1.0" encoding="utf-8"?>
      <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="match_parent">
      
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintTop_toTopOf="parent">
      
              <TextView
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:textSize="32sp"
                  android:ellipsize="end"
                  android:maxLines="1"
                  android:layout_weight="1"
                  tools:text="Lorem ipsum dolor sit amet, consectetur"/>
      
              <androidx.appcompat.widget.AppCompatImageView
                  android:id="@+id/ivInfectedDevOtherInfoIcon"
                  android:layout_width="40dp"
                  android:layout_height="40dp"
                  android:layout_marginStart="8dp"
                  app:layout_constraintTop_toTopOf="@+id/ivInfectedDevOtherName"
                  app:layout_constraintStart_toEndOf="@+id/ivInfectedDevOtherName"
                  app:layout_constraintEnd_toEndOf="parent"
                  app:layout_constraintHorizontal_bias="0"
                  app:srcCompat="@drawable/ic_add" />
      
          </LinearLayout>
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

      • 这适用于文本较长且被截断的情况。当文本很短时,图像保持停靠在右侧,并且两个元素之间的间距大于预期的 8dp
      • @Raimundo 尝试更新的答案。看看它是否适合你。
      • 这种方式有效。但是@MariosP 答案也有效,它更适合我的需求。谢谢。
      【解决方案3】:

      试试这个方法……

      <?xml version="1.0" encoding="utf-8"?>
      <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="match_parent">
      
      <LinearLayout
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintTop_toTopOf="parent">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_weight="8"
              android:layout_height="wrap_content"
              android:textSize="32sp"
              android:ellipsize="end"
              android:maxLines="1"
              tools:text="Lorem ipsum dolor sit amet, consectetur"/>
      
          <androidx.appcompat.widget.AppCompatImageView
              android:id="@+id/ivInfectedDevOtherInfoIcon"
              android:layout_width="0dp"
              android:layout_weight="2"
              android:layout_height="40dp"
              android:layout_marginStart="8dp"
              app:layout_constraintTop_toTopOf="@+id/ivInfectedDevOtherName"
              app:layout_constraintStart_toEndOf="@+id/ivInfectedDevOtherName"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0"
              app:srcCompat="@drawable/ic_tv" />
      
        </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

      • 当文本较短时,图像停留在右侧,两个元素之间的间距大于预期的 8dp。
      • 还是不行。当文本很短时,图像保持停靠在右侧,并且没有 40dp 的大小。 android:layout_weight 似乎不是解决这个问题的方法。
      • 您可以在 textView Widget 中添加属性 android:maxLength="20"
      • 但是右边有一些空间没有使用。而且这种方式不适用于所有屏幕尺寸和密度。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 2012-01-07
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多