Android - 文本图像右侧水平重叠屏幕
Android - Image Right of Text Overlaps Screen Horizontally
我正在尝试在 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>
当文本很长时,如何使图像停靠在右侧以使其不重叠?
编辑 1: 来自评论:
This works when the text is long and is truncated. When the text is
short the image stays docked at the right and there is a bigger gap
then the intended 8dp between the two element
因此,除非将视图直接插入 TextView
中,否则您正在寻找的行为将不起作用,它将测量当前宽度并在到达 drawable
末尾时截断& 不是文字。所以你可以创建这样的组件或使用 drawableEnd
或 drawableEndCompat
.
使用 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=1
和 layout_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>
试试这个方法...
<?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>
只需对您的 xml 布局进行以下更改:
1.In 线性布局 添加:
android:layout_width="wrap_content"
app:layout_constraintHorizontal_bias="0"
2.In TextView 添加:
android:layout_weight="1"
3.In 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>
长文本结果:
小文本结果:
我正在尝试在 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>
当文本很长时,如何使图像停靠在右侧以使其不重叠?
编辑 1: 来自评论:
This works when the text is long and is truncated. When the text is short the image stays docked at the right and there is a bigger gap then the intended 8dp between the two element
因此,除非将视图直接插入 TextView
中,否则您正在寻找的行为将不起作用,它将测量当前宽度并在到达 drawable
末尾时截断& 不是文字。所以你可以创建这样的组件或使用 drawableEnd
或 drawableEndCompat
.
使用 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=1
和 layout_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>
试试这个方法...
<?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>
只需对您的 xml 布局进行以下更改:
1.In 线性布局 添加:
android:layout_width="wrap_content"
app:layout_constraintHorizontal_bias="0"
2.In TextView 添加:
android:layout_weight="1"
3.In 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>
长文本结果:
小文本结果: