检测ListView中的TextView是否为椭圆化

时间:2011-05-02 09:29:42

标签: android android-layout android-listview textview

我有一个自定义适配器,可以在ListView中呈现一些项目。如果项目的文本是椭圆形的,我需要在ListView的项目上显示一个图标,如果有足够的空间让文本完成,我需要隐藏它。我可以访问我的适配器的getView方法中的按钮(我设置文本),但是在设置文本时不会立即添加省略号。

我有什么方法可以做到这一点吗?

这是我的TextView标记:

<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:ellipsize="end"
          android:singleLine="true"
          android:id="@+id/list_item_description"/>

4 个答案:

答案 0 :(得分:13)

  

public int getEllipsisCount (int line)

     

返回要椭圆化的字符数,如果不进行省略则返回0。

所以,只需致电:

if(textview1.getLayout().getEllipsisCount() > 0) {
   // Do anything here..
}

由于在设置布局之前无法调用getLayout(),因此请使用ViewTreeObserver查找加载textview的时间:

ViewTreeObserver vto = textview.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
       Layout l = textview.getLayout();
       if ( l != null){
          int lines = l.getLineCount();
          if ( lines > 0)
              if ( l.getEllipsisCount(lines-1) > 0)
                Log.d(TAG, "Text is ellipsized");
       }  
    }
});

最后,当你需要它时,不要忘记删除 removeOnGlobalLayoutListener

答案 1 :(得分:8)

棘手的部分是,您在getView中使用的视图有时会被布局,有时则不会,因此您必须处理这两种情况。

如果尚未布局,则设置视图树观察器以检查省略号。对于回收视图,布局已经存在,您可以在设置文本后立即检查省略号。

public View getView(int position, View convertView, ViewGroup parent) {
  final ViewHolder vh;
  if (convertView == null) {
    vh = new ViewHolder();
    ... // create/inflate view and populate the ViewHolder
  }
  vh = (ViewHolder) convertView.getTag();

  // Set the actual content of the TextView
  vh.textView.setText(...);

  // Hide the (potentially recycled) expand button until ellipsizing checked
  vh.expandBtn.setVisibility(GONE);

  Layout layout = vh.textView.getLayout();
  if (layout != null) {
    // The TextView has already been laid out
    // We can check whether it's ellipsized immediately
    if (layout.getEllipsisCount(layout.getLineCount()-1) > 0) {
      // Text is ellipsized in re-used view, show 'Expand' button
      vh.expandBtn.setVisibility(VISIBLE);
    }
  } else {
    // The TextView hasn't been laid out, so we need to set an observer
    // The observer fires once layout's done, when we can check the ellipsizing
    ViewTreeObserver vto = vh.textView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        Layout layout = vh.textView.getLayout();
        if (layout.getEllipsisCount(layout.getLineCount()-1) > 0) {
          // Text is ellipsized in newly created view, show 'Expand' button
          vh.expandBtn.setVisibility(VISIBLE);
        }

        // Remove the now unnecessary observer
        // It wouldn't fire again for reused views anyways
        ViewTreeObserver obs = vh.textView.getViewTreeObserver();
        obs.removeGlobalOnLayoutListener(this);
      }
    });

  return convertView;
}

答案 2 :(得分:1)

我希望我能正确理解您的问题 - 如果您希望结束对于带有省略号的屏幕来说太宽的TextView,您可以将这些属性添加到TextView

android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"

但是,如果您要确定TextView是以省略号结尾还是完全显示,我不太确定是否可能 - 它看起来不像。不过,您可能还想尝试getEllipsize() TextView方法。我不确定这是否会返回Android对其进行椭圆化处理的点,或将TextView设置为椭圆化的位置。

答案 3 :(得分:-1)

您可以将文字设置为选框。 在你的代码中添加这个可能有帮助.....

    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollHorizontally="true"
    android:freezesText="true"
    android:marqueeRepeatLimit="marquee_forever"

您还可以为您编写水平滚动视图....

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/list_item_description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:ellipsize="end"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>
    </HorizontalScrollView>
相关问题