Textview 没有截断文本

Textview is not truncating the text

以下是我的 .xml 文件

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical" >

    ...
    <TextView
        android:id="@+id/tvAlternateRoad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/fancy_dashboard_max_speed_circle_strock_size"
        android:gravity="start"
        android:text="Via A11"
        android:lines="1"
        android:textColor="#808080"
        android:textSize="@dimen/fancy_dashboard_lable_size" />

</TableRow>

我在主视图中调用上面的 .xml 文件 .xml 如下,

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.85"
                android:gravity="end"
                android:orientation="horizontal" >

                <TableLayout
                    android:id="@+id/layAlternateRoute"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_margin="@dimen/fancy_dashboard_max_speed_circle_strock_size"
                    android:background="@drawable/bg_round_rect_alternate_route" />
            </RelativeLayout>

http://i.stack.imgur.com/a0KRs.png

我在屏幕上显示一个视图,上面有 TableRow 元素。我在 运行 时间设置此 Textview tvAlternateRoad 的文本值。有时,当 Text 很大时,它会移出视图。我尝试使用 S.O 中的 android:ellipsize="end" 对其进行分类 运行。帖子,但似乎现在几天都不起作用。

有人可以帮我解决这个问题吗?

将 paddingRight 赋予文本视图。此外,文本视图具有宽度换行内容,与任何其他视图无关。因此,根据文本长度,您的文本视图宽度可以超过圆角背景图像。因此,要么给出一些 maxWidth 值,要么将其与其他一些视图相关联。

编辑

我刚做了这个xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
       android:id="@+id/textview_Statu"
            style="@style/text_view_login_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/more_txt3_margin_t"
            android:background="@android:color/white"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="1234567890qwertyuiopasdfghjklzxcvbnm"
            android:textColor="@color/blue_end" />
  </LinearLayout>

在图形视图中,它看起来像这样

它工作正常。

我注意到 TextView 会根据其所在的容器在包装和截断方面做不同的事情。例如,尝试让 TableRow 单元格成为 FrameLayout.然后将您的 TextView 放入 FrameLayout 中,看看会发生什么。

试试这个

表格布局应该是

<TableLayout
 android:id="@+id/tbl"
 android:layout_height="wrap_content"
 android:layout_width="match_parent"
  >

剩下的部分应该是

 <TableRow 
      android:layout_height="wrap_content"
      android:layout_width="match_parent"

      >

 <TextView


        android:id="@+id/tvAlternateRoad"


     android:layout_height="wrap_content"
            android:layout_width="match_parent"

            android:ellipsize="end"
    android:maxLines="1"
    android:minLines="1"

                android:layout_marginRight="@dimen/fancy_dashboard_max_speed_circle_strock_size"
                android:gravity="start"
                android:text="Via A11"
               android:layout_weight="1"
                android:textColor="#808080"
                android:textSize="@dimen/fancy_dashboard_lable_size" />

只需使用: android:singleLine="true" 和 android:ellipsize="end" 并为您的文本视图设置固定宽度,例如 "match_parent" 或 dp 中的某些内容,例如“180dp”等。 singleLine="true" 需要你的 textview 有一个固定的宽度,而 "wrap_content" 没有提供。 文本视图只会在其视图边界的末尾显示省略号,并且由于您已经设置 "wrap_content" 您的文本视图没有任何固定边界。

好的,经过长时间的努力,我自己解决了这个问题。

逻辑:

  • 以编程方式获取屏幕尺寸
  • int screen_width = context.getResources().getDisplayMetrics().widthPixels;
  • 然后我使用 screen_width 的 42% 作为 textView
  • 的最大宽度
  • ((TextView)alternate.findViewById(R.id.tvAlternateRoad)).setMaxWidth((int)(screen_width*0.42));
  • 我还在 xml
  • 中设置了以下值
  • android:椭圆大小="marquee"
  • android:maxLines="1"