【问题标题】:android:ellipsize="end" , "..." centered verticallyandroid:ellipsize="end" , "..." 垂直居中
【发布时间】:2013-11-10 15:18:53
【问题描述】:

我有一个奇怪的问题,由于某种原因,android:ellipsize="end" 有效,但在文本中间添加了点 == 垂直居中而不是与基线对齐:

我检查了任何“中心”属性,但没有一个:

更新: 这是 XML 部分:

 <com.citylifeapps.cups.customviews.CarmelaTextView
        android:id="@+id/venue_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/venue_distance"
        android:layout_toRightOf="@+id/venue_name"
        android:gravity="left"
        android:text="@string/placeholder_venue_address"
        android:textColor="@color/cups_white"
        android:textSize="20sp"
        android:textStyle="bold"
        android:ellipsize="end"
        android:singleLine="true"
        android:scrollHorizontally="true"
        android:layout_alignBaseline="@+id/venue_name" />

还有自定义的TextView 类:

public class CarmelaTextView extends TextView {
public CarmelaTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setCarmelaTypeface(context);
}

public CarmelaTextView(Context context) {
    super(context);
    setCarmelaTypeface(context);
}

private void setCarmelaTypeface(Context context) {
    if (this.isInEditMode()) return;
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "carmela.ttf");
    this.setTypeface(typeface);
}
}

进一步检查表明,如果我使用简单的TextView,问题就会消失, 但自定义 TextView 中没有任何内容会导致这种行为。

有谁知道为什么会发生这种情况?

谢谢。

【问题讨论】:

  • 请分享你的xml
  • @BaapJamesBondKaBaap,已添加。

标签: android android-layout textview


【解决方案1】:

看起来问题出在我用于此自定义 TextView 的自定义字体中,来自此处接受的答案:

Why does TextView in single line elipsized with "end" show boxes?

我猜我面临同样的问题,但结果不同,因为我的字体的 3 个点 (...) U+FEFF 字形不同。

但是,如果有人找到适用于这个问题的解决方案,我会很高兴他能分享它。

【讨论】:

    【解决方案2】:

    我使用这个类来解决这个问题

    公共类 EllipsizingTextView 扩展 TextView { 私有静态最终字符串 ELLIPSIS = "...";

    public interface EllipsizeListener {
        void ellipsizeStateChanged(boolean ellipsized);
    }
    
    private final List<EllipsizeListener> ellipsizeListeners = new ArrayList<EllipsizeListener>();
    private boolean isEllipsized;
    private boolean isStale;
    private boolean programmaticChange;
    private String fullText;
    private int maxLines = -1;
    private float lineSpacingMultiplier = 1.0f;
    private float lineAdditionalVerticalPadding = 0.0f;
    
    public EllipsizingTextView(Context context) {
        super(context);
    }
    
    public EllipsizingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.maxLines });
        setMaxLines(a.getInt(0, 1));
    }
    
    public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.maxLines });
        setMaxLines(a.getInt(0, 1));
    }
    
    public void addEllipsizeListener(EllipsizeListener listener) {
        if (listener == null) {
            throw new NullPointerException();
        }
        ellipsizeListeners.add(listener);
    }
    
    public void removeEllipsizeListener(EllipsizeListener listener) {
        ellipsizeListeners.remove(listener);
    }
    
    public boolean isEllipsized() {
        return isEllipsized;
    }
    
    @Override
    public void setMaxLines(int maxLines) {
        super.setMaxLines(maxLines);
        this.maxLines = maxLines;
        isStale = true;
    }
    
    public int getMaxLines() {
        return maxLines;
    }
    
    @Override
    public void setLineSpacing(float add, float mult) {
        this.lineAdditionalVerticalPadding = add;
        this.lineSpacingMultiplier = mult;
        super.setLineSpacing(add, mult);
    }
    
    @Override
    protected void onTextChanged(CharSequence text, int start, int before,
            int after) {
        super.onTextChanged(text, start, before, after);
        if (!programmaticChange) {
            fullText = text.toString();
            isStale = true;
        }
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        if (isStale) {
            super.setEllipsize(null);
            resetText();
        }
        super.onDraw(canvas);
    }
    
    private void resetText() {
        int maxLines = getMaxLines();
        String workingText = fullText;
        boolean ellipsized = false;
        if (maxLines != -1) {
            Layout layout = createWorkingLayout(workingText);
            if (layout.getLineCount() > maxLines) {
                workingText = fullText.substring(0,
                        layout.getLineEnd(maxLines - 1)).trim();
                while (createWorkingLayout(workingText + ELLIPSIS)
                        .getLineCount() > maxLines) {
                    workingText = workingText.substring(0,
                            workingText.length() - 1 - 1);
                }
    
                workingText = workingText + ELLIPSIS;
                ellipsized = true;
            }
        }
        if (!workingText.equals(getText())) {
            programmaticChange = true;
            try {
                setText(workingText);
            } finally {
                programmaticChange = false;
            }
        }
        isStale = false;
        if (ellipsized != isEllipsized) {
            isEllipsized = ellipsized;
            for (EllipsizeListener listener : ellipsizeListeners) {
                listener.ellipsizeStateChanged(ellipsized);
            }
        }
    }
    
    private Layout createWorkingLayout(String workingText) {
        return new StaticLayout(workingText, getPaint(), getWidth()
                - getPaddingLeft() - getPaddingRight(), Alignment.ALIGN_NORMAL,
                lineSpacingMultiplier, lineAdditionalVerticalPadding, false);
    }
    
    @Override
    public void setEllipsize(TruncateAt where) {
        // Ellipsize settings are not respected
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2013-10-15
      • 2012-12-29
      • 2011-07-07
      • 2014-02-27
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多