7,Absolute Layout(Absolutelayout)
Absolute Layout: A viewgroup displays its child view elements in an absolute way, that is, it locates on the screen in coordinates.
This layout method is easy to understand. You can set the coordinates of the view in the layout file or programmatically to locate the view absolutely. Layout file:
<AbsolutelayoutXmlns: Android = "http://schemas.android.com/apk/res/android" Android: Id = "@ + ID/absolutelayout01" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: id = "@ + ID/txtintro" Android: text = "absolute layout" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"Android: layout_x = "20dip" <! -- Have an eye on! --> Android: layout_y = "20dip"> <! -- Have an eye on! --></Textview></Absolutelayout>
It's easy. I am not going into it here!
8. Tab Layout)
Label Layout: A viewgroup displays its child view elements as tags, just like Displaying Multiple webpages in a window in Firefox.
To create a label UI (tabbed UI), you need to useTabHostAndTabwidget.TabHostMust be the root node of the layout, which containsTabwidget andFrameLayout.
There are two ways to implement TAG content: use tags to exchange views in the same activity, and use tags to change between fully isolated activities. Select a different method based on your needs. However, if each TAG provides different user activities, select isolated activities for each tag, therefore, you can better manage applications with separated groups, rather than a huge application and layout.The following is an example to create a tag UI. Each tag uses an isolated activity.
1) create three isolated activity classes in the project: artistisactivity, albumactivity, and songactivity. Each of them represents a separate label. Each simple message is displayed through textview, for example:
public class ArtistsActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textview = new TextView(this); textview.setText("This is the Artists tab"); setContentView(textview); }}
The other two classes are similar.
2) set the icon of each label. Each icon should have two versions: one is selected, and the other is not selected. It is recommended that the selected icon be dark (Gray) and the unselected icon be light (white ).
Now create a state-list drawable to specify which icon represents the label state: Put the image in the Res/drawable directory and create a new XML file namedic_tab_artists.xml, The content is as follows:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- When selected, use grey --> <item android:drawable="@drawable/ic_tab_artists_grey" android:state_selected="true" /> <!-- When not selected, use white--> <item android:drawable="@drawable/ic_tab_artists_white" /></selector>
3) set Res/layour/Main. XML to the following:
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dp" /> </LinearLayout></TabHost>
This layout displays the tab and provides the navigation between the activities created above.TabHostA tabwidget andFrameLayout. Tabwidget andFrameLayoutTabHostIt is displayed linearly and vertically.
4). The source code of the helloworld. Java file is as follows:
package skynet.com.cnblogs.www;import android.widget.TabHost;import android.app.TabActivity;import android.content.Intent;import android.content.res.Resources;import android.os.Bundle;public class HelloWorld extends TabActivity{ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Resources res = getResources(); // Resource object to get Drawables TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Resusable TabSpec for each tab Intent intent; // Reusable Intent for each tab // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, ArtistsActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("artists").setIndicator("Artists", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent); tabHost.addTab(spec); // Do the same for the other tabs intent = new Intent().setClass(this, AlbumsActivity.class); spec = tabHost.newTabSpec("albums").setIndicator("Albums", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent); tabHost.addTab(spec); intent = new Intent().setClass(this, SongsActivity.class); spec = tabHost.newTabSpec("songs").setIndicator("Songs", res.getDrawable(R.drawable.ic_tab_artists)) .setContent(intent); tabHost.addTab(spec); tabHost.setCurrentTab(2); }}
Set the text and icons for each label and assign an activity for each label (the three labels have the same icon for convenience ). Tabhost referencesGettabhost. Then, createTabhost. tabspec defines the attributes of a tag.newTabSpec(String)Method to create a newTabHost.TabSpecIdentifies a tag with a given string. CallTabHost.TabSpec,setIndicator(CharSequence, Drawable)Set text and icons for each label and callsetContent(Intent)SpecifyIntentOpen a suitable activity. EachTabHost.TabSpecBy callingAddtab (tabhost. tabspec) is added to tabhost.
Finally,setCurrentTab(int)Set the position of the index tag to enable the default display.
5) Open the android inventory file androidmanifest. xml and addNotitlebar topic to helloworld<Activity> tag. This removes the title and top layout of the default application and makes room for the tag.<activity>The tag should be like this:
<activity android:name=".HelloWorld" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar">
What results can you get by running this program? Please check by yourself. However, I will tell you that it may fail to run, and the error "Java. Lang. nullpointerexception" is reported! Many people who want to run this example will have this problem. Don't believe it!
PS: in fact, this is also a bug of Android, and this bug has not been solved in 2.2. Many people around the world have encountered this problem and listed it in http://code.google.com/p/android/issues.
Next, let's look ......
If you read this article, you will be lucky! After my hard debugging and information retrieval, I found the solution:
Add the following three activities to the androidmanifest. xml file:
<Activity Android: Name = ". albumsactivity" Android: Label = "@ string/app_name"> </activity>
<Activity Android: Name = ". artistsactivity" Android: Label = "@ string/app_name"> </activity>
<Activity Android: Name = ". songsactivity" Android: Label = "@ string/app_name"> </activity>
Now you can see the following results:
Figure 9 label Layout