Android App Development Basics (4)-----tabhost (tab)
Source: Internet
Author: User
<span id="Label3"></p><p><p>Link Address: http://www.cnblogs.com/lknlfy/archive/2012/02/18/2357093.html</p></p><p><p><strong>I. Overview</strong></p></p><p><p>Tabhost is a component used to display labels, not clear? Take a look at the <strong>call-through</strong> application to Know. This component is not used in the same way as other components, it needs to inherit the tabactivity class, and its layout files are different from what we usually use.</p></p><p><p><strong>second, The requirements</strong></p></p><p><p>Implement a tab with three tabs, each with a different layout as the content, prompting the user when switching between different labels.</p></p><p><p><strong>third, the realization</strong></p></p><p><p><strong>Disclaimer: This application needs to use the previous (3) related Content.</strong></p></p><p><p>Create a new project Myui (this name casually), Modify The/res/layout/main.xml file, add 3 linearlayout as 3 label Layouts.</p></p><p><p>The complete Main.xml file is as follows, note that line 2nd is no longer using linearlayout, but uses Tabhost.</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>1 <?xml version= "1.0" encoding= "utf-8"?><br>2 <tabhost xmlns:android= "http://schemas.android.com/apk/res/android"<br>3 android:id= "@android: id/tabhost"<br>4 android:layout_width= "fill_parent"<br>5 android:layout_height= "fill_parent" ><br>6<br>7 <linearlayout<br>8 android:id= "@+id/tab1"<br>9 android:orientation= "vertical"<br>Ten android:layout_width= "fill_parent"<br>android:layout_height= "fill_parent" ><br>12<br><listview<br>Android:id= "@+id/list1"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content"<br>><br>18<br></ListView><br></LinearLayout><br>21st<br><linearlayout<br>android:orientation= "vertical"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "fill_parent"<br>><br>27<br></LinearLayout><br>29<br><linearlayout<br>Android:id= "@+id/tab2"<br>android:orientation= "vertical"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "fill_parent" ><br>35<br>Approx. <textview<br>PNS android:id= "@+id/other"<br>Android:layout_width= "fill_parent"<br>android:layout_height= "wrap_content"<br>android:text= "this is a tab app"<br>Android:textsize= "20px"<br>Android:textcolor= "#FF0000"<br>android:gravity= "center_horizontal"<br>/><br>45<br></LinearLayout><br>47<br></TabHost></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Then create a new Adapter1.java file, write an inherited baseadapter Adapter1 class, relatively simple, you can see the basic article (3) of the relevant content, the complete Adapter1.java file as Follows:</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>1 package com.nan.ui;<br>2<br>3 Import android.content.Context;<br>4 Import android.graphics.Color;<br>5 Import android.view.LayoutInflater;<br>6 Import android.view.View;<br>7 Import android.view.ViewGroup;<br>8 Import android.widget.BaseAdapter;<br>9 Import android.widget.TextView;<br>10<br>11<br>public class Adapter1 extends Baseadapter<br>13 {<br>+ Private Context mcontext;<br>The private int num;<br>16<br>Adapter1 (Context c, int N)<br>18 {<br>Mcontext = c;<br>num = n;<br>21}<br>22<br>@Override<br>public int GetCount ()<br>25 {<br>/TODO auto-generated Method Stub<br>Return num;<br>28}<br>29<br>@Override<br>Public Object GetItem (int arg0)<br>32 {<br>/TODO auto-generated Method Stub<br>arg0 return;<br>35}<br>36<br>Panax Notoginseng @Override<br>Getitemid Public Long (int arg0)<br>39 {<br>Max//TODO auto-generated Method Stub<br>arg0 return;<br>42}<br>43<br>@Override<br>Public View GetView (int arg0, view arg1, viewgroup arg2)<br>46 {<br>//TODO auto-generated Method Stub<br>48<br>Arg1 = Layoutinflater.from (mcontext). Inflate (r.layout.tab1, null);<br>TextView T1 = (TextView) Arg1.findviewbyid (r.id.name);<br>Wuyi T1.settext ("ted");<br>T1.settextcolor (color.blue);<br>TextView t2 = (TextView) Arg1.findviewbyid (r.id.num);<br>T2.settext ("13709394939");<br>55<br>Arg1 return;<br>57}<br>58<br>59}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>We see that the 49th line of code above uses the Tab1.xml layout file, so create a new Tab1.xml file and add 2 TextView in it, as Follows:</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 <?xml version= "1.0" encoding= "utf-8"?><br><br>3 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>4 android:layout_width= "fill_parent"<br>5 android:layout_height= "fill_parent"<br>6 android:orientation= "vertical"<br>7 ><br><br>9 <textview<br>Ten android:id= "@+id/name"<br> android:layout_width= "wrap_content"<br> android:layout_height= "wrap_content"<br> /><br><br> <textview<br> android:id= "@+id/num"<br> android:layout_width= "wrap_content"<br> android:layout_height= "wrap_content"<br> /><br><br></LinearLayout></pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Continue to create a Activity1.java file, the contents of which is the most basic things we usually use, do not parse, the content is as Follows:</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 package com.nan.ui;<br><br>3 Import android.app.Activity;<br>4 Import android.os.Bundle;<br><br><br>7 public class Activity1 extends Activity<br>8 {<br><br>Ten /** called when the activity is first Created. * *<br> @Override<br>Public void OnCreate (Bundle Savedinstancestate)<br> {<br> super.oncreate (savedinstancestate);<br> Setcontentview (r.layout.activity1);<br><br>+ }<br><br>19}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Create a layout file named Activity1.xml for the above file with only 1 textview, as Follows:</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 <?xml version= "1.0" encoding= "utf-8"?><br><br>3 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<br>4 android:layout_width= "fill_parent"<br>5 android:layout_height= "fill_parent"<br>6 android:orientation= "vertical"<br>7 ><br><br>9 <textview<br>Ten android:id= "@+id/mtextview"<br> android:layout_width= "fill_parent"<br> android:layout_height= "wrap_content"<br> android:text= "this is an activity"<br> android:textsize= "20px"<br> android:gravity= "center_horizontal"<br> android:textcolor= "#0000FF"<br> /><br><br></LinearLayout></pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p><br>It's not over yet, and the final part of this article Is----myuiactivity.java File. The Myuiactivity class in the file inherits the Tabactivity class,</p></p><p><p>First instantiate the Tabhost object, then use Main.xml as the layout file, set the content to display for 3 tags, and finally set the monitoring of the tag toggle action, using the Displaytoast () function to display the current switch to the first few tags. For a detailed comment, see the following complete code:</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>1 package com.nan.ui;<br>2<br>3<br>4 Import android.app.TabActivity;<br>5 Import android.content.Intent;<br>6 Import android.os.Bundle;<br>7 Import android.view.LayoutInflater;<br>8 Import android.widget.ListView;<br>9 Import android.widget.TabHost;<br>Ten Import android.widget.Toast;<br>Import android.widget.TabHost.OnTabChangeListener;<br>12<br>13<br>public class Myuiactivity extends tabactivity<br>15 {<br>16//declaration Tabhost Object<br>+ Private Tabhost mtabhost;<br>18<br>Private Adapter1 ad1;<br>Private ListView ListView1;<br>* Private Intent mintent;<br>22<br>/** called when the activity is first Created. */<br>@Override<br>public void OnCreate (Bundle Savedinstancestate)<br>26 {<br>Super.oncreate (savedinstancestate);<br>//setcontentview (r.layout.main);<br>29<br>30//set display 4 rows in listview<br>AD1 = new Adapter1 (myuiactivity.this,4);<br>32<br>mintent = new Intent ();<br>34//set activity to start<br>Mintent.setclass (myuiactivity.this, activity1.class);<br>36<br>37//get Tabhost Object<br>Mtabhost = Gettabhost ();<br>39//use tabhost Layout<br>Layoutinflater.from (this). Inflate (r.layout.main, mtabhost.gettabcontentview (), true);<br>41//set the contents of the first label (using the ListView as Content)<br>Mtabhost.addtab (mtabhost.newtabspec ("1"). setindicator ("list", null). setcontent (r.id.tab1));<br>43//set the contents of the second label (use activity as Content)<br>Mtabhost.addtab (mtabhost.newtabspec ("2"). setindicator ("page", null). setcontent (mintent));<br>45//set the contents of the third label (using a simple linear layout as Content)<br>Mtabhost.addtab (mtabhost.newtabspec ("3"). setindicator ("other", null). setcontent (r.id.tab2));<br>47<br>ListView1 = (ListView) Mtabhost.findviewbyid (r.id.list1);<br>49//set the contents of the ListView to be displayed<br>Listview1.setadapter (ad1);<br>51<br>52//tag Switching event Handling<br>Mtabhost.setontabchangedlistener (new Ontabchangelistener ()<br>54 {<br>/TODO auto-generated Method Stub<br>@Override<br>ontabchanged public void (String TabId)<br>58 {<br>59//add Handling Events Yourself<br>If (tabid== "1")<br>61 {<br>Displaytoast ("Tab1");<br>63}<br>+ Else if (tabid== "2")<br>65 {<br>Displaytoast ("Tab2");<br>67}<br>Or else<br>69 {<br>Displaytoast ("Tab3");<br>71}<br>72}<br>73});<br>74}<br>75<br>private void Displaytoast (String S)<br>77 {<br>Toast.maketext (myuiactivity.this, s, toast.length_short). show ();<br>79}<br>80<br>81}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>finally, declare one more activity under the Androidmanifest.xml file, as Follows:</p></p><p><p></p></p><pre><pre>1 <activity<br>2 Android:name= ". Activity1 "<br>3 ><br>4 </activity></pre></pre><p><p>ok, Run the program, the beginning of the interface is as Follows:</p></p><p><p></p></p><p><p></p></p><p><p>Switch to the second tab, as Follows:</p></p><p><p></p></p><p><p></p></p><p><p>Switch to the third tab, as Follows:</p></p><p><p></p></p><p><p>OK, Finish.</p></p><p><p></p></p><p><p>Android App Development Basics (4)-----tabhost (tab)</p></p></span>
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