首页 > 解决方案 > 如何使 textview drawableright 始终与字符串的最后一个字符对齐

问题描述

我正在开发一个项目,该项目需要一个 textview 视图,其右侧有一个可绘制对象,或者 textview 右侧有一个 imageview 视图。问题陈述是图像应该始终与中心的文本对齐。如果文本是单行,那么一切看起来都很好,但是当文本是多行文本时,图像将保持在两行的中心。我尝试了多种方法,包括丰富的drawables和verticleimage span,但都没有帮助。在图像中,第一个和第二个是我到目前为止所得到的,但第三个是它应该是预期的结果(即使第一个是正确的,如果它是单行文本)。在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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="@dimen/media_item_poster_width"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/browse_media_button"
    android:background="@drawable/poster_overlay_selector"
    android:clickable="true"
    android:focusable="true"
    android:padding="@dimen/media_browser_item_padding">

<TextView
    android:id="@+id/view_all_number_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/media_poster"
    android:layout_marginStart="@dimen/media_item_poster"
    android:layout_marginBottom="@dimen/media_item_poster"
    android:ellipsize="end"
    android:maxWidth="@dimen/browse_view_all_number_tv_max_width"
    android:maxLines="2"
    android:textColor="@color/anz_text_color_primary"
    android:textSize="@dimen/media_item_poster_view_all"
    app:mainResourcePkgName="@string/resource_pkg_name_global"
    tools:text="48 Titles" />

<ImageView
    android:id="@+id/view_all_arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_view_all_arrow"
    android:layout_alignBottom="@+id/media_poster"
    android:layout_marginBottom="@dimen/media_item_poster_view_all"
    android:layout_toEndOf="@+id/view_all_number_tv"
    android:layout_marginStart="@dimen/view_all_arrow_margin_start"
    android:layout_marginEnd="@dimen/media_item_poster_view"
    app:mainResourcePkgName="@string/resource_pkg_name_module"
    app:labelsResourcePkgName="@string/resource_pkg_name_global" />

</RelativeLayout>

标签: javaandroidxml

解决方案


推荐阅读