簡體   English   中英

如何在滑動標簽布局中將圖標和文本對齊到center_vertical

[英]How to align icon and text to the center_vertical in sliding tab layout

我正在嘗試將文本和圖標與“ Sliding Tab Layoutcenter(Vertical Center)對齊。 我怎樣才能做到這一點 ?

@Override
public CharSequence getPageTitle(int position) {
    Drawable image = mContext.getResources().getDrawable(imageResId[position]);
    image.setBounds(0, 0, image.getWidth(), image.getHeight());
    // Replace blank spaces with image icon
    SpannableString sb = new SpannableString("   " + tabs[position]);
    ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}

圖片

嘗試閱讀以下網址,可能您的問題將得到解決。

圍繞ImageSpan中心垂直對齊文本

試試下面的代碼:

 @Override
public CharSequence getPageTitle(int position) {
    Drawable image = mContext.getResources().getDrawable(imageResId[position]);
    image.setBounds(0, 0, image.getWidth(), image.getHeight());
    // Replace blank spaces with image icon
    SpannableString sb = new SpannableString("   " + tabs[position]);
    ImageSpan imageSpan = new CenteredImageSpan(getApplicationContext(), imageResId[position]); 
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}


public class CenteredImageSpan extends ImageSpan {

private WeakReference<Drawable> mDrawableRef;

public CenteredImageSpan(Context context, final int drawableRes) {
    super(context, drawableRes);
}

@Override
public int getSize(Paint paint, CharSequence text,
                   int start, int end,
                   Paint.FontMetricsInt fm) {
    Drawable d = getCachedDrawable();
    Rect rect = d.getBounds();

    if (fm != null) {
        Paint.FontMetricsInt pfm = paint.getFontMetricsInt();
        // keep it the same as paint's fm
        fm.ascent = pfm.ascent;
        fm.descent = pfm.descent;
        fm.top = pfm.top;
        fm.bottom = pfm.bottom;
    }

    return rect.right;
}

@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
                 int start, int end, float x,
                 int top, int y, int bottom, @NonNull Paint paint) {
    Drawable b = getCachedDrawable();
    canvas.save();

    int drawableHeight = b.getIntrinsicHeight();
    int fontAscent = paint.getFontMetricsInt().ascent;
    int fontDescent = paint.getFontMetricsInt().descent;
    int transY = bottom - b.getBounds().bottom +  // align bottom to bottom
            (drawableHeight - fontDescent + fontAscent) / 2;  // align center to center

    canvas.translate(x, transY);
    b.draw(canvas);
    canvas.restore();
}

// Redefined locally because it is a private member from DynamicDrawableSpan
private Drawable getCachedDrawable() {
    WeakReference<Drawable> wr = mDrawableRef;
    Drawable d = null;

    if (wr != null)
        d = wr.get();

    if (d == null) {
        d = getDrawable();
        mDrawableRef = new WeakReference<>(d);
    }

    return d;
}
}

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM