Android development skills-use style for Custom Controls
Android development skills-A Review of using style for Custom Controls
In the previous "Android development tips-custom properties of custom controls", I learned how to define attributes and obtain the values of these attributes in custom controls, we also mentioned that these attributes can be specified in the layout file or in the topic. Next, we will share the two methods I have learned about specifying attribute values in a topic.
Specify attribute values in a topic
During the development process, we learned how to specify the value of a custom control in the layout file to meet different requirements. However, we sometimes encounter a situation where we want to make a global configuration for the properties of a widget so that when I use it in this project, the same performance does not require each layout file to copy the attribute value once. In another project, we can perform another global configuration.
Attribute definition and implementation
In the previous article, we talked about custom attributes as follows:
WhereformatDefines the format of this attribute. It supports the following methods:
booleanBoolean Value
colorColor
dimensionDimensions
enumEnumeration
flagBitwise OR operation
floatFloating point value
fractionPercentage
integerInteger value
stringString
referenceReference a resource ID
You can also specify multiple formats when defining the format. For example
What I want to talk about here isreferenceTo reference a resource ID.
We can define an attribute in the formatreferenceAnd thenthemeSet its value tostyleIn this way, we can read thisstyle. This is our reflection on this implementation process.
The following uses IconTabPageIndicator, a project I previously wrote, as an example. For all the code, see its develop branch. This is a menu indicator at the bottom, where each tab (inherited from TextView) is expected to be defined in style. First, we define an attribute to specify the style of the tab:
The following describes how to use this attribute.
Obtain from Java code
Override the constructor. In this example, TabView is new in java code, and the constructor is called.TabView(Context context)So we need to override this constructor and call it in this constructor.this(context, null, R.attr.tabView)The third parameter is passed in R. attr. tabView, which is the style attribute we define.
public TabView(Context context) { this(context, null, R.attr.tabView); }
Then we will rewrite the calleddefStyleParameter constructor, because another constructorTextView(Context context, AttributeSet attrs)It is also called:
public TabView(Context context, AttributeSet attr, int defStyle) { super(context, attr, defStyle); TypedArray a = context.obtainStyledAttributes(attr, R.styleable.TabView, defStyle, 0); iconWidth = a.getDimensionPixelSize(R.styleable.TabView_iconWidth, 0); iconHeight = a.getDimensionPixelSize(R.styleable.TabView_iconHeight, 0); a.recycle(); }
In the constructor, the first line is to call the parent constructor. Next, we need to obtain other custom attributes. For example, in this example, the icon width and height are different from those used yesterday.
obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)The third parameter is the style attribute we define, and the fourth parameter is the style resource. When determining the final value of an attribute, the priority order is as follows:
First, obtain the attribute value in the specified AttributeSet. If the attribute value cannot be found, the style in AttributeSet will be obtained.
style=@style/xxxx) If the specified resource cannot be found, go
defStyleAttrAnd
defStyleResIn the default style. Finally, find the current
theme.
Therefore, in the above method, we can also pass a default style (R. style. xxx). When the user does not declare some attributes in the style specified by the third attribute, the attribute in the style in our fourth parameter will be used.
Before callingobtainStyledAttributesAfter obtaining the attributes, you can refer to the previous blog or the project for how to use the attributes.
How Others use
In theory, when others use it, they only need to write a style and specify it in the topic of their application. However, we usually need to specify many attribute values, and these attribute values are common in most cases. So firstA good style should be provided, As follows:
In this style, I defined the tab text center, background color, font size color, margin, and so on. When the attributes are not overwritten, a good appearance can be provided.
Then writethemeAnd specify this style as an example for other developers:
The tabView here is the style attribute we define and is also specified in the constructor.R.attr.tabView.
If the library user needs to modify the style, you only need to write a style that inherits fromTabViewStyle, override the attribute values:
Of course, there is also the most important step:Must be specified in the topic of your project:
For a complete project in this example, refer to the Github project IconTabPageIndicator
Specify the style attribute in the layout File
In addition to using Java code to obtain and use the style attribute we define, we can also use it directly through layout files. YesstyleAttribute.
For a widgetstyleAttribute, usually written in the following way:style=@style/xxxxxOr specify the attributes in the android system:style=@android:style/xxxxIn addition, we can also specify the style attribute we define.
This method is used in the ActionSheet project of another exercise. For the complete code, see the Project address on github. Here, only some relevant code is extracted.
ActionSheet is a menu I wrote that looks a bit like qq menus. I hope each item of menus can be configured. Menus are loaded by Dialog, while Dialog is new in Java code. Therefore, you cannot configure the menu appearance by defining attributes in xml.
First, some attributes are defined, which is the same as the above:
The attribute of the menu list, the style of the cancel button, and the style of each menu. In our menu layout file, except for the attribute that cannot be configured, we declare the attribute.styleAttribute, writtenStyle =? Attr/the property name you defined, As follows:
We also need to define these styles in our style. xml file:
The following code is omitted...
Database users also need to specify these attribute values when using them:
This article ends with the following items:
IconPageIndicator: I learned from ViewPagerIndicator ActionSheet of the great god of JakeWharton. I also forgot the project I learned from.