How to Set controls for android

Source: Internet
Author: User

 

Sometimes we are not satisfied with changing the background and text color of the system control. These attributes can be changed in the code, A large string of Methods Starting with set will be provided later. However, it is troublesome to change the code in this way. If there are more controls, there will be more repeated code. These attributes of the control can also be modified in the xml file. If there are more controls, the code will still be duplicated. In this case, we can use the method of setting controls. The following uses the EditText control as an example to illustrate how to set the style of a control. The source code of the Work Program can be downloaded later;

Step 1 create a new project and create three EditText controls in the main layout.

Step 2: Create an xml file in the res/values folder of the Project as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>

<Style name = "et1" parent = "@ android: style/Widget. EditText">

<Item name = "android: background"> #1A4EA4 </item>

<Item name = "android: textColor"> # FFF111 </item>

</Style>

<Style name = "et2" parent = "@ android: style/Widget. EditText">

<Item name = "android: background"> # A6C60F </item>

<Item name = "android: textColor"> # EC02C3 </item>

</Style>

</Resources>

<! --

Note:

Two styles are created here. We can see from the literal meaning that both styles have changed the background color and text color respectively.

There are many attributes that a control can change. Here, only two attributes are changed. To change other attributes, we need to know the attribute name,

. Where are the names of all the attributes? After searching, we finally found that all the attribute names are stored in one attrs. xml

File. This file exists in the \ data \ res \ values directory of the android sdk. After finding this file, we

Some attributes can be used as materials for viewing.

The preceding two styles have a parent attribute. It is not difficult to understand that style can be inherited. Here we follow

The default EditText attribute only modifies the background and text color. Where is the EditText style of the system?

What is the definition? After searching, I finally found out that the style of all system controls is in a style. xml file.

In the directory mentioned above. We can find the style definition of Widget. EditText. With it, we can also view

Which styles can be changed for a widget.

@ And? are frequently used when setting styles? @ Indicates that the referenced resource is defined in a project or system framework.

.? Indicates that the referenced resource is defined in the current topic.

In addition to the color value, the Set background value can also be an image reference ~~.

 

-->

 

 

Step 3: Modify the EditText attribute in the main layout file. For comparison, we will change the first attribute and the second style attribute to the style created above with name et1. The third style attribute is set to the style created above with name et2. The Code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent"

Android: orientation = "vertical">

<EditText android: layout_width = "fill_parent"

Android: layout_height = "wrap_content" android: text = "Default style" android: id = "@ + id/et1"> </EditText>

<EditText android: layout_width = "fill_parent" android: id = "@ + id/et2"

Android: text = "Custom style 1" android: layout_height = "wrap_content" style = "@ style/et1"> </EditText>

<EditText android: layout_width = "fill_parent" android: id = "@ + id/et3"

Android: text = "Custom style 2" android: layout_height = "wrap_content" style = "@ style/et2"> </EditText>

<Button android: text = "change topic" android: id = "@ + id/btn1"

Android: layout_width = "fill_parent" android: layout_height = "wrap_content"> </Button>

</LinearLayout>

 

It is invalid to add a button to change the topic. It will be used in the next article to set the topic. Although the style file is defined, it does not seem to be set in java code. I have not found methods like setStyle. Therefore, the style file cannot be used in java code to dynamically change the style of the Control. I don't know which of the following friends can solve this problem.

No picture, no truth, the following is:

 

Package project files: Click me

 

Author: bausch

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.