Android specifies that the DatePickerDialog style does not display the year’s implementation code

There are many places to use DatePickerDialog. But sometimes the theme style used in the project is very ugly, which is really ugly.

And the style we really want is like this.

This is much more beautiful. In addition, it is not necessary to display "year" in many cases. Let's see that you can specify the theme to display the style you want, and hide the "year" through the reflection mechanism. You can draw inferences from one example and modify the tasks above.

When instantiating DatePickerDialog, the second parameter specifies that the topic is this_ HOLO_ Light is the style you want, and there is a very interesting place. You can pass any value of type int to see what messy themes your phone supports. Of course, it's just for fun, or you can write the specified theme regularly.

Try {} catch() {} began to use reflection to obtain the layout of "year". Because the field of year in the source code is decorated with private and is not provided by the get method, getdeclarefield () should be used to obtain this field Then unpack, otherwise you can't get it, that is, setaccessible (true), which means that it is accessible. At this time, what you get is not the value of the current instance, but a field information in this class. The last sentence is file Get (object) is to get the field value owned by the current instance. Of course, it is the displayed "year". You can operate on this, and I just need to hide it. Some people have read on the Internet that it is not readable to obtain layer by layer through layout, and there is a great possibility that different versions can be changed. In this way, the "year" can not be obtained. Relatively speaking, it should be less likely to change the field name.

End, this is just to provide a way of thinking, there may be a better way, don't spray!

The above Android specified DatePickerDialog style does not show that the implementation code of the year is all that Xiaobian shared with you. I hope it can give you a reference and support more programming tips.

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
分享
二维码
< <上一篇
下一篇>>