[Android Analysis] android Program Interface Programming and View Components

Source: Internet
Author: User
Tags xml attribute

The main content of android Application Development is interface development. With the increasing popularity of mobile devices, android applications are almost everywhere and are designed in various fields. For users, in addition to focusing on the functions of an application, the graphic interface is also the most important object. If an application does not provide a friendly GUI, it will be difficult to attract end users. On the contrary, if an application provides a friendly GUI ), you can easily click events with your fingers to love you and operate the application. Then you will feel "awesome". Just like windows systems, it was initially able to quickly attract a large number of users because of its rich graphic interfaces that almost monopolized the entire market. It is conceivable how important a friendly graphic interface is.

For programmers, apart from developing a friendly graphical interface, it is better to think about how to implement it. Android provides a large number of UI components with rich functions. These components and functions have certain rules and many similarities. Developers only need to master these rules, you can achieve a beautiful and friendly GUI. In the next period of time, we will introduce them one after another. Today, let's first introduce the connection between Interface Programming and view components!

1. View components and container Components

In an activity file, we can see many packages:

 

import android.widget.EditText;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.PopupWindow;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RelativeLayout;import android.widget.SeekBar;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;import android.widget.Toast;import android.widget.ViewSwitcher;

Most of the UI components of android applications are stored in the android. widget package, its sub-packages, the android. view package, and its sub-packages. It is worth noting that all components in android inherit the View class. The View class is a very important class. It also has a subclass ViewGroup, but the ViewGoup class is often used as the container of other components.

Another note is the relationship between the View and ViewGoup classes. The View class includes the ViewGoup class and the ViewGoup class also includes the View class:

How can we implement interface development in the android project? Generally, we can achieve this in two ways:

(1) control by XML attributes in XML layout files. This method is generally recommended.

(2) control by calling methods in Java program code.

Regardless of the implementation method, they are essentially the same. To implement the UI component in an XML file, we add the corresponding XML attributes of the UI component. To implement the UI component in java code, we control the attributes of the component through the corresponding attribute methods. That is to say, in fact, the XML Attribute of each UI component corresponds to a method. The attributes of the UI component include common XML attributes and related methods of the View class and common XML methods in the ViewGoup class.

For View classes, they are the base classes of all UI components, so the XML attributes they contain correspond to a method. Commonly used:

 

XML attributes Related Methods Description
Android: alpha SetAlpha (float) Set the transparency of components
Android: background SetBackGroundResource (int) Set the background color of the component.
Android: id SetId (int) Sets the unique identifier of a component.
Android: keepScreenOn SetKeepSCreenOn (int) Set whether to force the mobile phone screen to open
Android: visibility SetVilibility (int) Set whether the component is visible

The ViewGroup class inherits the View class, so it can be used as a common View class. However, the ViewGroup class is mainly used as a container class. Because the ViewGroup class is an abstract class, some of its subclasses are usually used as containers, such as various layout managers. The ViewGroup container controls the distribution of its child components in two internal classes: Viewgroup. LayoutParams and ViewGroup. MarginLayoutParams. Both internal classes provide some XML attributes. The child components in the ViewGroup container can specify these XML attributes. Viewgroup. LayoutParams supports two XML attributes:
XML attributes Description
Android: layout_height Height of the Child widget Layout
Android: layout_width Specify the layout width of the Child widget.
Use the following in the XML layout file:
android:layout_width="fill_parent"android:layout_height="wrap_content"
Android: layout_height and android: layout_width attributes have three values: (1) and fill_parent: the height and width of the sub-component are the same as the height and width of the parent container (in fact, the padding distance is also subtracted ). (2) match_parent: This attribute is exactly the same as fill_parent, but this attribute is recommended for later versions of android2.2. (3) wrap_parent: specify the size of the component to wrap the content in it.

In addition, ViewGroup. MarginLayoutParams supports four XML attributes:

 

 

XML attributes Related Methods Description
Android: layout_marginBottom SetMargins (int, int) The margin of the Child widget.
Android: layout_marginLeft SetMargins (int, int) The margin on the left of the Child widget.
Android: layout_marginRight SetMargins (int, int) The margin on the Right of the Child widget.
Android: layout_marginTop SetMargins (int, int) The margin above the child widget.

 

Use the following in the XML layout file:

 

android:layout_marginTop="10dp" android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginBottom="10dp"

Of course, the attributes of components are far more than that. There are many other attributes that are listed above!

2. Use an XML layout file to control the UI

 

This implementation method is often used and recommended in android. It is simple, convenient, and clear, and the View control logic of the application can be separated from the java code, put it into the XML file for control, which is a good example of the MVC principle. Before that, we also talked about how to implement components in XML files. Next we will briefly introduce the following:

(1) set the layout component in the XML file:

For example, I implement a Text control:

 

 

 

 

Next we need to find the layout file in Activity first:

 

setContentView(R.layout.main);
To access this component, You need to bind its ID to know which component to access:

 

 

private Text showText;
showText = (Text) findViewById(R.id.show_helloworld);
After obtaining the specified UI component, you can use the code to control the attributes of the component and the listening events bound to the UI component.

 

(2) control the UI in the code

Although it is recommended that you use XML files to control components in android, you can control a component in java code if needed, and you can completely discard the XML file restrictions.

Next we will connect to the above implementation. Assuming that the above main interface is implemented through an XML file, we will adjust it to another Activity page by clicking the Text control, this Activity page is implemented through java code.

Create a JavaActivity class and add code:

 

// Create a linear layout manager LinearLayout layout = new LinearLayout (this); // set anctilayout to display layoutsuper. setContentView (layout); layout. setOrientation (LinearLayout. VERTICAL); // create a TextViewfinal TextView textView = new TextView (this); textView. setText ("I implemented it through java code ...... "); Layout. addView (textView );

In the MainActivity class, call:

 

 

showText = (Button) findViewById(R.id.show_helloworld);showText.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent intent = new Intent(MainActivity.this,JavaActivity.class);startActivity(intent);}});

Effect

 


From the code above, we can see that components in the javaactivity class are created using the new keyword, and then the program uses the LineLayout container to "Store" these components, thus forming a graphical user.

In many cases, we have to pass this parameter. Why? This is because a Content parameter is input when the UI component is created, and Content indicates the API that accesses the global information of the android application environment. Let the UI component hold a Content parameter so that these UI components can obtain the global information of the android application environment through the Content parameter. Content itself is an abstract class. android application components such as Activity and server all inherit Content. Therefore, Activity and Server can be directly used as Content.

In addition to the above two methods to control the UI components, we can also use them in combination, so that we can develop more flexibly! You can create a new project by yourself!

Well, today we are going to end it. Do you think it is very simple? If you look at the number of times, you can try your own code = to discover more problems, in order to think more deeply. Next time, we will continue to introduce new knowledge-four basic la s for android + new la S. You are welcome to continue to stay tuned. Let's talk and make progress together ......







 


 

 

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.