The width of textview in Android programming is too large, so drawable cannot be centered. Solution

This article describes the solution to the problem that drawable cannot be centered due to the excessive width of textview in Android programming. Share with you for your reference, as follows:

When doing projects, we often use text and pictures to display together. Generally, we can set the drawableleft, drawableright, drawabletop and drawablebottom of textview. But there is a situation when the familiarity of textview is fill_ When parent or weight is used and the ceter of gravity is set, drawable pictures cannot be centered together. In order to solve this problem, we usually set another layer of layout, and then set the familiarity of textview to wrap_ Content, but sometimes when too many layouts are nested, stackoverflow may occur, so it must be optimized. Let's talk about one of the solutions. First, the figure above

This solution is very rough and has great limitations. The text can't wrap. It's not allowed after wrapping. Here's the source code:

XML layout:

Calling code:

In fact, there are more convenient methods. The following friends learn from the code of a netizen (I don't know the address):

XML layout:

Well, I wrote it myself and learned something. You can discuss what better method you have.

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>