【问题标题】:ellipsize on textview doesnt worktextview 中的 ellipsize 不起作用
【发布时间】:2014-03-14 10:52:47
【问题描述】:

如果我的 textview 长度太长,我想显示省略号。但由于某种原因,它似乎不起作用。请看一下图像的外观:

这是我的布局代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView 
          android:id="@+id/txt1_trans"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textIsSelectable="true"
          android:bufferType="spannable"
          android:layout_marginLeft="10dp"
          android:singleLine="true"
          android:ellipsize="end"
          android:freezesText="true"
          android:focusable="true" 
          android:focusableInTouchMode="true"
          android:text="My Personal > Bank of America savings"
          android:textStyle="bold"
          android:textSize="18sp"
          android:layout_marginTop="5dp"
          android:textColor="#000000"/>

<TextView 
    android:layout_width="wrap_content" 
    android:id="@+id/TextView2"
    android:layout_height="wrap_content" 
    android:textSize="18sp"
    android:text="100$"
    android:textStyle="bold"
    android:layout_marginRight="20dp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/txt1_trans"
    android:layout_alignBottom="@id/txt1_trans"/>

</RelativeLayout>

如果左侧的 textView (txt1_trans) 占用右侧的 textview ("TextView2") 占用的空间,我希望它显示省略号。请让我知道我做错了什么。

谢谢

【问题讨论】:

    标签: android android-layout textview ellipsis


    【解决方案1】:

    我发现布局有问题。对于我布局上的第二个 TextView,我设置了

    alignParentRight = true

    第一个 TextView 有

    width = "wrap_content"

    因此,这两个 TextView 最终都会占用公共空间。为了避免这种情况,我将第一个 TextView 设置为_LeftOf 2nd TextView 。这确保他们不会尝试在同一个公共空间上书写。这是我的正确布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    <TextView 
          android:id="@+id/txt1_trans"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textIsSelectable="true"
          android:bufferType="spannable"
          android:layout_marginLeft="10dp"
          android:singleLine="true"
          android:ellipsize="end"
          android:layout_toLeftOf="@+id/TextView2"
          android:freezesText="true"
          android:focusable="true" 
          android:focusableInTouchMode="true"
          android:text="My Personal Bank of America savings"
          android:textStyle="bold"
          android:textSize="18sp"
          android:layout_marginTop="5dp"
          android:textColor="#000000"/>
    
       <TextView 
          android:layout_width="wrap_content" 
          android:id="@+id/TextView2"
          android:layout_height="wrap_content" 
          android:textSize="18sp"
          android:text="100$"
          android:textStyle="bold"
          android:layout_marginRight="20dp"
          android:layout_alignParentRight="true"
          android:layout_alignTop="@id/txt1_trans"
          android:layout_alignBottom="@id/txt1_trans"/>
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      你不能使用 ellipsize 和 wrap_content 作为宽度。您需要使用 wrap_parent 或最好使用 match_parent,具体取决于您所针对的 Android 版本。

      【讨论】:

      • 没有wrap_parent这样的属性——你的意思是match_parent或fill_parent
      • 是的,我很抱歉 fill_parent
      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 2012-06-30
      • 2018-05-29
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      相关资源
      最近更新 更多