android - 如何不使用 Long TextView 将 ImageView 推离屏幕?
问题描述
我希望我的图像视图直接留在我的文本视图的右侧。我可以通过在 TextView 的属性中设置toLeftOf来完成此操作。但是对于非常长的文本字符串,我设置了省略号,并且在达到布局的宽度后,它也会将 ImageView 推开。
我可以通过将 TextView 的重力设置为 1 来显示ImageView,但随后 ImageView 锚定在右侧。我该怎么办?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left">
<TextView
android:id="@+id/title_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:layout_weight="1"
android:text="MY TEXT TO THE LEFT"
android:layout_toLeftOf="@id/img_button" />
<ImageButton
android:id="@+id/img_button"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginLeft="4dp"
android:layout_marginTop="6dp"
android:background="@drawable/some_img"
app:srcCompat="@drawable/ic_edit" />
</LinearLayout>
因此,设置权重 = 1,将图像拥抱到最右边,我需要它是动态的,并且始终位于 textview 的右侧,然后当 textview 的宽度变得比父视图长时,对于图像不会被推离屏幕。
解决方案
您可以使用constraintLayout并将您的 textView 宽度设置为0dp
(也称为match constraint
)。
如果文本很多,结果将导致您的 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/textView8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@android:color/holo_green_light"
android:padding="10dp"
android:text="something"
android:textColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="@+id/textView10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline3"
app:layout_constraintTop_toTopOf="@+id/textView10" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<TextView
android:id="@+id/textView10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="this is really long text and it wont push the green image because of android:layout_width attribute"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
它看起来像这样:
推荐阅读
- typescript - 变量中的 mapGetters['getters'] (Decorator, ts, vue)
- php - 使用作曲家时出现分段错误
- python - 在运行 python 的 ubuntu 中找不到文件
- javascript - express 应用程序没有给出任何响应,但数据已保存到 mongodb。ERR_HTTP_HEADERS_SENT
- microsoft-graph-api - 如何在 microsoft-teams graph api 中获得“已看到”聊天消息的状态?
- sbt - scalac-scoverage-runtime 上的交叉版本冲突
- php - 有没有办法在@property-read 中使用对象类型并“声明”它的属性?
- javascript - 在 JavaScript 应用程序的 Opentelemetry 中添加服务名称
- java - 在 Jersey 的 ResourceConfig 中获取单个 context.xml 的值?
- javascript - Chrome 扩展开/关切换按钮