Custom interface style and Android interface style for android development and learning

Source: Internet
Author: User

Custom interface style and Android interface style for android development and learning

A unified user interface makes applications more friendly. To unify the user interface, we must use style and theme ). The OPhone system provides many default system styles and themes, but in many cases, these cannot meet our needs. For example, we don't always want the background color to be defined by the system, and we don't want the font size to be the same. Of course, we can modify it in every space, but if we put it in the style, it is easier to unify the user interface. If you do not know what style is or what theme is, you can find a detailed explanation in the Dev Guide document of Andoird, this article describes how developers can customize their own styles and themes to meet developers' requirements and make the user interface easier to implement.

In this article, we will use a simple example to demonstrate how to customize the style gradually. It mainly uses a custom class TextView. By modifying the style set to TestView, you can learn how to customize the style step by step.

 

(1) Simple customization and application Style

 

When querying the Dev Guide document of OPhone, developers may find that the document introduces a simple style customization. This simple style customization can meet the needs of many users. For example, you only want to modify the background color or font size of the View. All the items you can modify are defined by the system. developers can change the value of items to achieve different effects. Example 1 is a simple customization. Create styles. xml in the values directory, and define the style name as Widget. TestView. Set the background color to white.

View plaincopy to clipboardprint?

<Resources>

<Style name = "Widget. TestView" parent = "android: Widget">

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

</Style>

</Resources> <span style = "font-style: italic; font-size: 10.5pt; font-family: 'times New Roman ';"> <br> </span>

 

Example 1

Example 1 shows how to customize the style. To apply the customized style to TestView In the example, you also need to specify this style when using TestView in the layout/testtheme. xml file. After this step is completed, run the sample program to see the white background effect. Example 2,
You can see that the statement style = "@ style/Widget. TestView" plays this role.

View plaincopy to clipboardprint?

<Com. android. mytest. TestView

Style = "@ style/Widget. TestView"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"/>

 

Example 2

(2) custom style entries

If the developer wants further development, he may find that only inheriting and modifying the entries defined by the system cannot meet his or her needs. For example, if a developer wants to display a colored circle on the customized TestView, it is clear that the entries defined by the system do not have information about the circle. So how to implement it? The simple customization described earlier cannot meet developers' needs.

 

For simplicity, we assume that TestView needs to understand the color and radius of the circle to draw a circle. Some people may disagree that we can simply write in the TestView code, and there is no problem in implementation. However, once modified, the user must modify the code, which may cause errors. It would be much easier if we put the information in our customized style. If you want to modify the color or size, you only need to modify the information of the xml file of the definition style without modifying the code.

 

Now, you have decided to add the circle information. First, add the circleColor and circleRadius entries in styles. xml and set their values. Example 3 shows how to add these two entries. Is that all right? If you compile it now, you will surely get the error message that the two entries do not exist. Why? The system does not define these two entries. We only use these two entries and do not define them. Of course, we do not know what the two entries are.

 

To let the system know that these two entries exist, we must first define them. Create an attrs. xml file under the values directory. Then add the circle radius and color.
In attrs. xml, you can define many types of items, such as integer, float, color, reference, and string. Here, only two types are defined: color and float. These types of information tell the OPhone Resource System how to interpret these fields,
For more information, see Example 4.

View plaincopy to clipboardprint?

<Resources>

<Style name = "Widget. TestView" parent = "android: Widget">

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

<Item name = "circleColor"> # FFFF0000 </item>

<Item name = "circleRadius"> 60 </item>

</Style>

</Resources>

Example 3

View plaincopy to clipboardprint?

<Resources>

<! -These are the attributes that make up customized theme.->

<Declare-styleable name = "TestView">

<! -Default TestView style.->

<Attr name = "testViewStyle" format = "reference"/>

<! -Color to use for the background of the Circle->

<Attr name = "circleColor" format = "color"/>

<Attr name = "circleRadius" format = "float"/>

</Declare-styleable>

</Resources>

Example 4

The definition is completed in attrs. xml and added in styles. xml. Now there should be no errors in compiling. The problem is how to let TestView know the information. These two entries are defined by ourselves, so we have to add some code to let TestView know about the information. The following example 5 shows how to find the two items from the total resource.

View plaincopy to clipboardprint?

Public TestView (Context context, AttributeSet attrs, int defStyle ){

Super (context, attrs, defStyle );

TypedArray a =

Context. obtainStyledAttributes (

Attrs, R. styleable. TestView, defStyle, 0 );

Int n = a. getIndexCount ();

For (int I = 0; I <n; I ++ ){

Int attr = a. getIndex (I );

Switch (attr ){

Case R. styleable. TestView_circleColor:

MCircleColor = a. getColor (attr, Color. RED );

Break;

Case R. styleable. TestView_circleRadius:

MCircleRadius = a. getFloat (attr, 40f );

Break;

}

}

}

Example 5

After obtaining the color and radius of the circle, TestView can use the information to draw the circle that the developer wants. For details about how to draw this colored circle, refer to the attachment code.

 

(3) set the default Style

As mentioned in the first part, if a developer wants to apply the TestView customized for you, the developer uses style = "@ style/Widget. TestView. If a developer uses TestView in his application, I think this is no big deal. However, if developers use TestView in many places, this method is a little troublesome. If you want to modify this style sometimes, it will be even worse. The following is a simple method to put your customized style into a custom topic. You have created a topic in the OPhone documentation. Careful readers may find that in example 4, in addition to the Circle information, there is another entry (testViewStyle), which is used here. In the development of this custom topic, set the value of this item to our customized style (Example 6). Remember that the style name is Widget. TestView? Now the customized style is the default style for this TestView.

View plaincopy to clipboardprint?

<Resources>

<Style name = "Theme. OMS" parent = "android: Theme">

<Item name = "testViewStyle"> @ style/Widget. TestView </item>

</Style>

</Resources>

 

Example 6

Then, the theme customized by the application is applied to the design application. The simple way is to add the statement android: Theme = "@ style/theme. OMS"> In AndroidManifest. xml. Now you can see the effect of your customized style. Compile and run it.

This article first describes how to customize a simple style, which can meet some requirements but cannot be further customized. For in-depth customization, developers must define their own entries and parse the entries. Finally, we will introduce how to make the custom style a default style of a View.


How to Develop Android?

After learning about Android, developers should give full play to the advantages of Android in their own software and make up for the defects or shortcomings of Android. Let's take a look at the features of Android:
It is very convenient to interact with hardware, including cameras and GPS.
It has its own runtime and virtual machines and excellent memory management capabilities.
Provides a wide range of interface controls for developers to use, allows visual development, and ensures that the application interfaces on the Android platform are consistent.
Provides a lightweight inter-process communication mechanism.
Supports unbounded background service applications.
Supports efficient and fast data access.
With the support of these features, it is not too difficult to develop an application in Android. In fact, developers with a little Java experience can quickly get started with Android development. The core of development has always been around the features of Android phones, including touch screens, cameras, GPS modules, Internet functions, voice input, and Google accounts. It should be said that if a J2SE Engineer wants to switch to Android, he will pay much more cost than J2SE or J2EE engineers. After all, Android supports the basic and complete J2SE subset. In turn, it will feel that its functions are too weak.
In addition to Java, there are many languages that support Android development. Scala is well-known. As the underlying language of Android, C/C ++ cannot be ignored. In the current open-source community, some cool people are trying to develop Android applications in more languages. The Representative is Koushik Dutta, which has solved the problem of requiring Dalvik to call Mono code on the Mono platform. Maybe in the near future, all languages in. NET may use Mono to run on Android, which is worth looking forward.
Language is no longer a problem, so what else will become a problem? Many may say "experience ". It is true that experience determines whether a developer can quickly and smoothly complete the development work. It also determines the robustness of the software, the number of bugs, the level, and the number of rework times of fixing problems. However, I don't think this is important. Even a newcomer in the Android industry can query documents and develop projects. Although the efficiency is low, the project can be completed in the same way. In Android, there are almost no bottlenecks in development technology. So where is the bottleneck? In fact, after using a lot of software, we will find that there are a lot of software is not easy to use. Many users do not want to use a software. It is not because the software has no technical content or cannot meet the requirements. The reason is very simple, that is, it is not easy to use.
The user experience is superior to the technology. It can be said that excellent user experience can get twice the result with half the effort. Among a bunch of similar software, the most downloads are available, it must make users feel the most comfortable to use, even if its functions are not better than other products, or even slightly worse. I have seen many developers who regard technology as their mission and focus only on technology. They think that software with excellent technology will be well received by users, even in a mobile game, add a variety of gorgeous and dazzling 3D effects. These are all good, but real users do not like them. In mobile apps, simplicity and clarity are what users want to see. Imagine that when a user plays an RPG Game on his mobile phone and is filled with the interface with gorgeous 3D effects, he will not be able to start the next action. It is true that gorgeous pictures are easy to attract people, but behind such gorgeous scenes, they will directly lead users and developers into an abyss, and they will no longer be able to look back. The final result is, users abandon the game completely, and developers or operators cannot make any money.
In the process of mobile platform development, user experience has become a top priority. Only by focusing on User Design can users be accepted by users. Here are some typical examples.
The figure on the left shows the classic Windows Mobile 6.1 interface. Since the launch of Windows Mobile, this interface has been promoted to enrich the content, including frequently-used websites ...... remaining full text>

What is the prospect of learning Android development?

The Android system has many versions and many application developers, but there are not many high-quality applications. The popular version is IOS transplantation. The development prospects of Android are not as hot as they seem now, because it is open-source and free, this is a good thing for consumers, but it is not a good thing for developers. without the support of economic sources, relying on advertising fees is far from enough, and as mentioned above, there are too many advertisements for apps that are difficult to produce good apps. Without a good app, the prospects for Android will not be very good. The current Android system requires a ROM that is pure, BUG-less, updatable, and stable, the second is to provide a formal and convenient application sales platform for Android Developers, and the price of this application should be appropriate and acceptable to consumers. Therefore, in general, there is no future for the chaotic development of the Android system according to the current situation. Scientific specifications are required. Only by referring to Apple's development model can there be prospects. The price of another small-scale mobile phone is reduced, so consumers will consider whether to invest money in app developers.

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.