简体   繁体   English

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

[英]How to add bottom padding to span content of textView within RelativeLayout programmatically

How can padding be added to the bottom of the textview programmatically? 如何以编程方式将填充添加到textview的底部? I noticed that there is some padding above the text but not below. 我注意到文本上方有一些填充,但下方没有。 What can be done in order to add some black padding at the bottom just like at the top? 为了像在顶部那样在底部添加一些黑色填充,可以做什么? It would make the text view a lot more legible (especially for letters that extend below the baseline ie g and y ). 这将使文本视图更加清晰(尤其是对于延伸到基线以下的字母, gy )。

在此处输入图片说明

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);

You can use: yourTextView.setPadding(0, 10, 0, 0); 您可以使用:yourTextView.setPadding(0,10,0,0); Android - How to set padding for TextView in ListView or ExpandableListView Android-如何在ListView或ExpandableListView中设置TextView的填充

Or increase the TextviewHeight and centre the text. 或增加TextviewHeight并使文本居中。

With this custom ReplacementSpan, text is drawn "vertically center" 使用此自定义的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 类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));
    }

}

在此处输入图片说明

Not exactly answer to "bottom padding" request but solve part of your problem I think. 不是完全回答“底部填充”请求,而是解决了部分问题。

( Original post ) 原始帖子

Set RelativeLayout.LayoutParams for Textview 为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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM