简体   繁体   中英

TextView ShadowLayer being Clipped when Using WRAP_CONTENT

It's been a while since I've posted and I've read many posts to try to resolve my issue but I just can't seem to figure it out. I am missing something and I hope someone can help me understand what I am doing wrong.

In this example I have a TextView inside a Linear Layout with the LayoutParams set to MATCH_PARENT. If I use WRAP_CONTENT with the TextView the Shadow is being clipped, even if I use Padding. If I set the TextView to MATCH_PARENT the shadow isn't clipped.

TextView WRAP_CONTENT No Padding: TextView WRAP_CONTENT无填充

TextView WRAP_CONTENT with Padding 带有填充的Textview WRAP_CONTENT

TextView MATCH_PARENT: TextView MATCH_PARENT

I've even tried it with ll.setClipToPadding(false); ll.setClipChildren(false);

What am I missing?

I don't think you're missing anything, unfortunately. This seems to be a bug with how TextView measures italic text. It's a problem even without shadows.

在此处输入图片说明

You'd have to subclass TextView (or AppCompatTextView) and define custom measuring behavior to work around this.

If you don't want to bother with that, the consensus "best" answer is to add a non-breaking space to the end of your text in order to increase the measured size of the text. Unfortunately, this often adds more space than you really want.

在此处输入图片说明

Incidentally, android:clipToPadding is only an attribute on ViewGroup , which is why it doesn't do anything when you apply it to a TextView (it is simply ignored, like any other unknown attribute).

This is probably really bad practice but I went with the following:

tv.setHeight(tv.getHeight() + tv.getPaddingTop() + tv.getPaddingBottom());
tv.setWidth(tv.getWidth() + tv.getPaddingLeft() + tv.getPaddingRight());
tv.setPadding(0, 0, 0, 0);

Result: 结果

Thanks Again Ben for helping me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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