簡體   English   中英

如何以編程方式在RelativeLayout中添加底部填充以覆蓋textView的內容

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

如何以編程方式將填充添加到textview的底部? 我注意到文本上方有一些填充,但下方沒有。 為了像在頂部那樣在底部添加一些黑色填充,可以做什么? 這將使文本視圖更加清晰(尤其是對於延伸到基線以下的字母, 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);

您可以使用:yourTextView.setPadding(0,10,0,0); Android-如何在ListView或ExpandableListView中設置TextView的填充

或增加TextviewHeight並使文本居中。

使用此自定義的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);

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

}

在此處輸入圖片說明

不是完全回答“底部填充”請求,而是解決了部分問題。

原始帖子

為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