【问题标题】:How to align icon and text to the center_vertical in sliding tab layout如何在滑动选项卡布局中将图标和文本与 center_vertical 对齐
【发布时间】:2016-05-04 07:11:22
【问题描述】:

我正在尝试将文本和图标与Sliding Tab Layout 中的center(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;
}

【问题讨论】:

标签: java android fragmentstatepageradapter


【解决方案1】:

尝试阅读以下网址,可能您的问题会得到解决。

Align text around ImageSpan center vertical

【讨论】:

    【解决方案2】:

    试试下面的代码:

     @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;
    }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多