文本在TextView中从底部切割

时间:2013-02-01 08:12:13

标签: android layout textview text-size

我为list.i创建了一个行布局,使用自定义textview来调整textize的大小。 但是文本正在从突出显示区域中显示的底部切割。 任何人都可以知道它为什么会发生。请给我一些解决方案。

public class AutoFitTextView extends TextView
{
private static final float THRESHOLD = 0.5f
private enum Mode { Width, Height, Both, None }

private int minTextSize = 1;
private int maxTextSize = 16;

private Mode mode = Mode.None;
private boolean inComputation;
private int widthMeasureSpec;
private int heightMeasureSpec;

public AutoFitTextView(Context context) {
    super(context);
}

public AutoFitTextView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public AutoFitTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray tAttrs = context.obtainStyledAttributes(
            attrs, R.styleable.com_techdeals_ui_widget_AutoFitTextView, defStyle, 0);
    maxTextSize = tAttrs.getDimensionPixelSize(
            R.styleable.com_techdeals_ui_widget_AutoFitTextView_maxTextSize, maxTextSize);
    minTextSize = tAttrs.getDimensionPixelSize(
            R.styleable.com_techdeals_ui_widget_AutoFitTextView_minTextSize, minTextSize);
    tAttrs.recycle();
}

private void resizeText() {
    if (getWidth() <= 0 || getHeight() <= 0)
        return;

    if(mode == Mode.None)
        return;

    final int targetWidth = getWidth();
    final int targetHeight = getHeight();

    inComputation = true;
    float higherSize = maxTextSize;
    float lowerSize = minTextSize;
    float textSize = getTextSize();
    while(higherSize - lowerSize > THRESHOLD) {
        textSize = (higherSize + lowerSize) / 2;
        if (isTooBig(textSize, targetWidth, targetHeight)) {
            higherSize = textSize; 
        } else {
            lowerSize = textSize;
        }
    }
    setTextSize(TypedValue.COMPLEX_UNIT_PX, lowerSize);
    measure(widthMeasureSpec, heightMeasureSpec);
    inComputation = false;
}

private boolean isTooBig(float textSize, int targetWidth, int targetHeight) {
    setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    measure(0, 0);
    if(mode == Mode.Both)
        return getMeasuredWidth() >= targetWidth || getMeasuredHeight() >= targetHeight;
    if(mode == Mode.Width)
        return getMeasuredWidth() >= targetWidth;
    else
        return getMeasuredHeight() >= targetHeight;
}

private Mode getMode(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    if(widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY)
        return Mode.Both;
    if(widthMode == MeasureSpec.EXACTLY)
        return Mode.Width;
    if(heightMode == MeasureSpec.EXACTLY)
        return Mode.Height;
    return Mode.None;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if(!inComputation) {
        this.widthMeasureSpec = widthMeasureSpec;
        this.heightMeasureSpec = heightMeasureSpec;
        mode = getMode(widthMeasureSpec, heightMeasureSpec);
        resizeText();
    }
}

@Override
protected int getSuggestedMinimumWidth() {
    Drawable background = getBackground();
    setBackground(null);
    int minWidth = super.getSuggestedMinimumWidth();
    setBackground(background);
    return minWidth;
}

@Override
protected int getSuggestedMinimumHeight() {
    Drawable background = getBackground();
    setBackground(null);
    int minHeight = super.getSuggestedMinimumHeight();
    setBackground(background);
    return minHeight;
}

public void setBackground(Drawable background) {
    /*if(Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN)
        super.setBackground(background);
    else*/
        setBackgroundDrawable(background);
}

@Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
    resizeText();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w != oldw || h != oldh)
        resizeText();
}

public int getMinTextSize() {
    return minTextSize;
}

public void setMinTextSize(int minTextSize) {
    this.minTextSize = minTextSize;
    resizeText();
}

public int getMaxTextSize() {
    return maxTextSize;
}

public void setMaxTextSize(int maxTextSize) {
    this.maxTextSize = maxTextSize;
    resizeText();
}

}

提前致谢。enter image description here

1 个答案:

答案 0 :(得分:0)

这里是xml文件

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright TechDeals 2012. All rights reserved. -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.techdeals"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/text_color_white"
android:padding="@dimen/padding_small_shopping_alert"
android:descendantFocusability="blocksDescendants" >

<ImageView
    android:id="@+id/imageView_logo"
    android:layout_width="@dimen/image_width_logo"
    android:layout_height="@dimen/image_height_logo"
    android:layout_centerVertical="true"
    android:layout_gravity="center"
    android:layout_margin="@dimen/margin_small_shopping_alert"
    android:layout_marginRight="@dimen/imageview_row_arrow_margin_right" >
</ImageView>

<com.techdeals.rest.ui.widget.CustomTextView
    android:id="@+id/textview_alert_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/textview_address_margin_left"
    android:layout_marginRight="@dimen/margin_small_shopping_alert"
    android:layout_toLeftOf="@+id/relative_layout_ImageViewandCheckBox"
    android:layout_toRightOf="@+id/imageView_logo"
    android:ellipsize="end"
    android:lines="1"
    android:maxLines="1"
    android:scrollHorizontally="true"
    android:textColor="@color/color_dark_teal"
    android:textSize="@dimen/text_size_medium"
    android:textStyle="bold"
    app:font="DroidSans-Bold.ttf" />

<com.techdeals.rest.ui.widget.CustomTextView
    android:id="@+id/textview_alert_type"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textview_alert_title"
    android:layout_below="@+id/textview_alert_title"
    android:layout_marginTop="@dimen/margin_small_shopping_alert"
    android:layout_toLeftOf="@+id/relative_layout_ImageViewandCheckBox"
    android:layout_toRightOf="@+id/imageView_logo"
    android:ellipsize="end"
    android:lines="1"
    android:maxLines="1"
    android:textColor="@color/color_light_green"
    android:textSize="@dimen/text_size_small"
    app:font="DroidSans.ttf" />

<com.techdeals.rest.ui.widget.CustomTextView
    android:id="@+id/textview_alert_method"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textview_alert_title"
    android:layout_below="@+id/textview_alert_type"
    android:layout_marginTop="@dimen/margin_small_shopping_alert"
    android:ellipsize="end"
    android:lines="1"
    android:maxLines="1"
    android:textColor="@color/color_light_brown"
    android:textSize="@dimen/text_size_small"
    app:font="DroidSans.ttf" />

<com.techdeals.rest.ui.widget.AutoFitTextView
    android:id="@+id/textview_alert_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textview_alert_type"
    android:layout_marginLeft="@dimen/margin_medium_shopping_alert"
    android:layout_toLeftOf="@+id/relative_layout_ImageViewandCheckBox"
    android:layout_toRightOf="@+id/textview_alert_method"
    android:lines="1"
    android:maxLines="1"
    android:textColor="@color/text_color_black"
    android:textSize="@dimen/text_size_small"
    app:font="DroidSans.ttf"
    android:padding="5dp"
    android:layout_alignBaseline="@+id/textview_alert_method" />

<RelativeLayout
    android:id="@+id/relative_layout_ImageViewandCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_gravity="center"
    >

    <ImageView
        android:id="@+id/imageView_row_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@drawable/row_arrow" >
    </ImageView>

    <CheckBox
        android:id="@+id/checkBox_row_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:visibility="gone"
        android:layout_centerVertical="true"
        android:focusableInTouchMode="false"/>
</RelativeLayout>