Android custom titleview Title Development example

In the process of Android development, we often encounter a project that needs to repeatedly define the title bar of the same style. Android has launched actionbar and toolbar one after another. I believe useful friends will also encounter some unhappy times. For example, when the title bar is centered, they need to customize the XML file to the toolbar. Those who do not know actionbar and toolbar can find the corresponding articles, Here's how the custom titlebar meets the domestic theme style.

In order to see the effect in advance, first put on the effect drawing:

preparation in advance

1. Predefine the ID for the title bar titleview, and select IDS under values In XML

Here you can see that the return button ID, title ID and back button ID on the left are defined. There are two cases on the left: ImageView / textview. There can be two buttons on the right, a combination of picture buttons and text buttons.

2. Customize the title bar attribute, and click attr. Under valuse In XML

• coding implementation

• with the previous preparations, you can now start coding and implementation. Here's how to introduce our custom controls into XML:

The title tag here is customized. First, create a class that inherits from relativelayout. Here, relativelayout is selected as the parent class container to facilitate relative position control.

First, we need to obtain the typedarray object through which the values of all custom attributes are obtained:

After you get the typedarray object, you can start adding buttons to the container. First look at the first return button on the left. If you add it, first look at the code:

Here left_ Text is a user-defined attribute, indicating the text displayed in the textview on the left. The text can be a predefined string in the application resource file or directly input text. After obtaining the corresponding styleable, first judge whether it is greater than 0. If it is greater than 0, it means it is defined in a string through typearray getResources(). Gettext() gets the value and takes the value directly when it is equal to 0, indicating that the value may be given by direct assignment. How to assign a value to textview after getting the value? Here, you need to give it width and height first, which is required by all controls

Let's write a separate method, which can be directly passed later

To get the preset width and height value. Here you can see that all the parent controls are filled with height, and the width is adaptive. Then there is a new textview:

Here you can see that we have passed in the preset ID value, the content to be displayed, and the layoutparams given above. After creating a textview, you can also set the drawable of the textview by customizing the attribute left_ text_ drawable_ right,left_ text_ drawable_ Left setting. Here, the left and right are set, and the corresponding up and down can be set:

Here is also a method extracted, and then through:

You can set drawable for the specified textview. After creating the textview, we mentioned earlier that we use relative layout and need to specify location rules:

It is displayed on the left here. At the same time, you can also set the font size through the custom attribute: small_ text_ Size (text size on both sides), title_ text_ Size to set the font:

Similarly, a separate method is written here. The usage of typedarray will not be discussed. You can see other articles. Then set the font size as follows:

Text color, the same principle:

Then call the method to set the color:

So far, the first text button on the left. Or the button with text and picture will be created. Finally, it is one step away:

For the same reason, other buttons can be added to the container in turn. I won't talk more. So far, the titleview we need is created, which can be called directly in the future. There is no need to repeat coding everywhere.

Project address: GitHub source code download

The above is an example of Android custom titleview Title Development introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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