The use of Android tabactivity
Last Update:2015-11-06
Source: Internet
Author: User
<span id="Label3"></p>Tabactivity for multi-page display effects<p><p>Because the mobile phone screen is limited, we should try our best to make full use of screen Resources. There are usually multiple activity in our application, and we often switch the display so that we can use tabactivity to display it. First look at the Effect:</p></p><p><p> </p></p><p><p>Let me lead you to the simplest implementation:</p></p><p><p>First of all our layout files:</p></p><pre><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"><span style="color: #000000;">Xmlns:tools</span></span>= "http://schemas.android.com/tools"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Tools:context</span></span>=". Activitytwo "> <<span style="color: #000000;"><span style="color: #000000;">tabhost Android:id</span></span>= "@+id/booktabhost"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content" > <<span style="color: #000000;"><span style="color: #000000;">LinearLayout Android:id</span></span>= "@+id/donebook"<span style="color: #000000;"><span style="color: #000000;">android:orientation</span></span>= "vertical"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content" > <<span style="color: #000000;"><span style="color: #000000;">TextView Android:text</span></span>= "border town"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> <<span style="color: #000000;"><span style="color: #000000;">TextView Android:text</span></span>= "fortress besieged"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> <<span style="color: #000000;"><span style="color: #000000;">TextView Android:text</span></span>= "the man Chasing the kite"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> </LinearLayout> <<span style="color: #000000;"><span style="color: #000000;">LinearLayout Android:id</span></span>= "@+id/doingbook"<span style="color: #000000;"><span style="color: #000000;">android:orientation</span></span>= "vertical"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content" > <<span style="color: #000000;"><span style="color: #000000;">TextView Android:text</span></span>= "love of the allure"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> <<span style="color: #000000;"><span style="color: #000000;">TextView Android:text</span></span>= "splendid Qianyang"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> <<span style="color: #000000;"><span style="color: #000000;">TextView Android:text</span></span>= "alive"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> </LinearLayout> <<span style="color: #000000;"><span style="color: #000000;">LinearLayout Android:id</span></span>= "@+id/willbook"<span style="color: #000000;"><span style="color: #000000;">android:orientation</span></span>= "vertical"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content" > <<span style="color: #000000;"><span style="color: #000000;">TextView Android:text</span></span>= "hundred years of loneliness"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> <<span style="color: #000000;"><span style="color: #000000;">TextView Android:text</span></span>= "the Elephant in the house"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> <<span style="color: #000000;"><span style="color: #000000;">TextView Android:text</span></span>= "confession"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> </LinearLayout> </TabHost></RelativeLayout></pre><p><p>Our main activity code:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">tabactivity{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Button button_two; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Tabhost bookth =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Bookth</span></span>=<span style="color: #000000;"><span style="color: #000000;">Gettabhost (); Layoutinflater.from (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>). Inflate (r.layout.activity_two, Bookth.gettabcontentview (),<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); Bookth.addtab (bookth.newtabspec (</span></span>"done"). Setindicator ("read"<span style="color: #000000;"><span style="color: #000000;">). setcontent (r.id.donebook)); Bookth.addtab (bookth.newtabspec (</span></span>"doing"). Setindicator ("positive read"<span style="color: #000000;"><span style="color: #000000;">). setcontent (r.id.doingbook)); Bookth.addtab (bookth.newtabspec (</span></span>"would"). Setindicator ("unread"<span style="color: #000000;"><span style="color: #000000;">). setcontent (r.id.willbook));</span></span><span style="color: #000000;"><span style="color: #000000;"> }}</span></span></pre><p><p>Ok we have finished the effect, the code is very simple, no more explanation. Let's take a look at another way to achieve this:</p></p><p><p>Our layout files:</p></p><pre><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"><span style="color: #000000;">Xmlns:tools</span></span>= "http://schemas.android.com/tools"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Tools:context</span></span>= "${relativepackage}.${activityclass}" > <<span style="color: #000000;"><span style="color: #000000;">tabhost Android:id</span></span>= "@android: id/tabhost"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent" > <<span style="color: #000000;"><span style="color: #000000;">LinearLayout android:orientation</span></span>= "vertical"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent" > <<span style="color: #000000;"><span style="color: #000000;">framelayout Android:id</span></span>= "@+id/framelayout"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">android:padding</span></span>= "5dp"/> <<span style="color: #000000;"><span style="color: #000000;">Tabwidget Android:layout_alignparentbottom</span></span>= "true"<span style="color: #000000;"><span style="color: #000000;">Android:id</span></span>= "@+id/tabwidget"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"/> </LinearLayout> </TabHost></RelativeLayout></pre><p><p>Our main activity code:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Activityone<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">tabactivity {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Resources Res</span></span>=<span style="color: #000000;"><span style="color: #000000;">getresources (); Tabhost Tabhost</span></span>=<span style="color: #000000;"><span style="color: #000000;">Gettabhost (); Tabhost.tabspec spec; Intent Intent; Layoutinflater.from (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>). Inflate (r.layout.activity_one, Tabhost.gettabcontentview (),<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); Intent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (). SetClass (activityone.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Activitytwo.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); Tabhost.addtab (tabhost.newtabspec (</span></span>"dial-up"). Setindicator ("dial"<span style="color: #000000;"><span style="color: #000000;">, res.getdrawable (<span style="color: #ff0000;">r.drawable.ic_tab_artists</span>)). setcontent (intent)); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Intent = new Intent (). SetClass (activityone.this,activitytwo.class);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">spec = Tabhost.newtabspec ("dialing"). setindicator ("dial", res.getdrawable (R.DRAWABLE.IC_TAB_ARTISTS))</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">. SetContent (intent);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Tabhost.addtab (spec);</span></span><span style="color: #000000;"><span style="color: #000000;">Intent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (). SetClass (activityone.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Activitythree.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); Spec</span></span>= Tabhost.newtabspec ("contacts"). setindicator ("contacts")<span style="color: #000000;"><span style="color: #000000;">, res.getdrawable (<span style="color: #ff0000;">r.drawable.ic_tab_albums</span>)). setcontent (intent); Tabhost.addtab (spec); Intent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (). SetClass (activityone.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>, Activityfour.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); Spec</span></span>= Tabhost.newtabspec ("call log"). setindicator ("call History")<span style="color: #000000;"><span style="color: #000000;">, res.getdrawable (<span style="color: #ff0000;">r.drawable.ic_tab_songs</span>)). setcontent (intent); Tabhost.addtab (spec); Tabhost.setcurrenttab (</span></span>1<span style="color: #000000;"><span style="color: #000000;">); }}</span></span></pre><p><p>Note the red font section, where a picture's configuration file (ic_tab_albums.xml) is used:</p></p><pre><pre><?xml version= "1.0" encoding= "UTF-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android > <!--when selected, use grey-to <item android:drawable= "@drawable/ic_tab_albums_grey" <span style="color: #000000;"> android:state_selected</span>= "true"/> <!--when not selected, use white--> <item Android :d rawable= "@drawable/ic_tab_albums_white"<span style="color: #000000;"> android:state_selected</span>= "false"/></selector></pre></pre><p><p>All right, Here we are about the introduction of Tabactivity completed, This part of the knowledge is not difficult, I believe we must have Mastered. Novice learning, Master Communication.</p></p><p><p>The use of Android tabactivity</p></p></span>