如何以编程方式在RelativeLayout中添加底部填充以跨越textView的内容

时间:2017-08-20 14:45:03

标签: java android textview spannablestring spannable

如何以编程方式将填充添加到textview的底部?我注意到文本上方有一些填充但不在下面。可以做些什么来在底部添加一些黑色填充,就像在顶部一样?它会使文本视图更加清晰(特别是对于延伸到基线以下的字母,即gy)。

enter image description here

TextView tv1 = v.findViewById(R.id.textView1);
String myString = " " + getString(R.string.magnify) + " ";
Spannable spanna = new SpannableString(myString);
spanna.setSpan(new BackgroundColorSpan(Color.BLACK), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanna.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.yellow)), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv2.setText(spanna);

3 个答案:

答案 0 :(得分:-1)

你可以使用:yourTextView.setPadding(0,10,0,0); Android - How to set padding for TextView in ListView or ExpandableListView

或者增加TextviewHeight并使文本居中。

答案 1 :(得分:-1)

使用此自定义ReplacementSpan,将绘制文本"垂直居中"

final SpannableString spannableString = new SpannableString(myString);
stringBuilder.append(spannableString);
stringBuilder.setSpan(new TagSpan(Color.BLACK, Color.MAGENTA), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
testTextView.setText(stringBuilder, TextView.BufferType.SPANNABLE);

Class TagSpan

public class TagSpan extends ReplacementSpan {
    private RectF mRect;
    private int mBackgroundColor;
    private int mForegroundColor;

    public TagSpan(int backgroundColor, int foregroundColor) {
        this.mRect = new RectF();
        this.mBackgroundColor = backgroundColor;
        this.mForegroundColor = foregroundColor;
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        // Background
        mRect.set(x, top, x + paint.measureText(text, start, end), bottom);
        paint.setColor(mBackgroundColor);
        canvas.drawRect(mRect, paint);

        // Text
        paint.setColor(mForegroundColor);
        int xPos = Math.round(x);
        int yPos = (int) (0 + ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)));
        canvas.drawText(text, start, end, xPos, yPos, paint);
    }

    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        return Math.round(paint.measureText(text, start, end));
    }

}

enter image description here

不完全回答"底部填充"请求,但我想解决你的部分问题。

Original post

答案 2 :(得分:-1)

为Textview设置RelativeLayout.LayoutParams

TextView tv1 = v.findViewById(R.id.textView1);
String myString = " " + getString(R.string.magnify) + " ";
Spannable spanna = new SpannableString(myString);
spanna.setSpan(new BackgroundColorSpan(Color.BLACK), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanna.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.yellow)), 0, myString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv1.setText(spanna);

RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, tv1.getId());
        tv1.setLayoutParams(params);