[Android Development Content Update category app] Iv. layout of basic functions of the Project (cont.)

Source: Internet
Author: User

Yesterday only to get the computer, do not say, are tears

http://joveth.github.io/funny/

1. Slide Effect of the tab you know, with this tab you want it to slide, otherwise, I don't like it. Before I let him slide, to say the previous question, saying, according to the designer download the package, replace to our res, I found that the tabhost option color has not changed, after I tried various methods, finally, I TM gave up, OK, just found the slide effect of the demo, There are also color transition effects for tabs. Speak no more, improve our things.
1. Modify our Acitivity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <tabhost android:id= "@+id/tab_hos            T "android:layout_width=" match_parent "android:layout_height=" match_parent "> <linearlayout Android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:orientation = "vertical" > <tabwidget android:id= "@android: Id/tabs" android:layout_width = "Match_parent" android:layout_height= "48DP" android:background= "@drawable/tab_widget_bg"/&            Gt <android.support.v4.view.viewpager android:id= "@+id/viewpager" android:layout_width= "MATC H_parent "android:layout_height=" Match_parent "/> <!--hidden--<framelayou            T    Android:id= "@android: Id/tabcontent" android:layout_width= "Match_parent" Android:layout_h eight= "Match_parent" android:visibility= "Gone" > <fragment android: Id= "@+id/fragment_image" android:name= "Com.jov.germany.frame.ImageFrame" Android:lay                    Out_width= "Match_parent" android:layout_height= "match_parent"/> <fragment                    Android:id= "@+id/fragment_text" android:name= "Com.jov.germany.frame.TextFrame" Android:layout_width= "Match_parent" android:layout_height= "match_parent"/> &L T;fragment android:id= "@+id/fragment_both" Android:name= "Com.jov.germany.frame.BothF             Rame "android:layout_width=" match_parent "android:layout_height=" Match_parent "/> </framelaYout> </LinearLayout> </TabHost></LinearLayout> 

This adds ViewPage, and hides the framlayout.
Where the layout files are:Res/color/tab_widget_text.xml
<?xml version= "1.0" encoding= "utf-8"?><!--tab Tab text Toggle color--><selector xmlns:android= "/http/ Schemas.android.com/apk/res/android ">        <item android:state_selected=" true "android:color=" @color/color_ Orange "/>        <item android:color=" #000 "/>    </selector>

added in Value/color.xml:
<color name= "Color_orange" > #FF9224 </color>

Why is orange, I ... If you don't like, Baidu HTML color code table, find a favorite of your own
In order to modify ourMainacitivity.java

Package Com.jov.germany;import Java.util.arraylist;import Java.util.list;import android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmentmanager;import Android.support.v4.view.pageradapter;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.view.menu;import Android.view.MenuItem; Import Android.view.view;import Android.view.viewgroup;import Android.widget.tabhost;import Android.widget.tabhost.ontabchangelistener;import Android.widget.tabhost.tabcontentfactory;import Android.widget.tabhost.tabspec;import Android.widget.textview;public class Mainactivity extends FragmentActivity { public static final String page1_id = "Page1";p ublic static final String page2_id = "Page2";p ublic static final string PAG e3_id = "Page3";p rivate tabhost tabhost; Tabhostprivate list<view> views; Collection of view objects within Viewpager private Fragmentmanager manager; Activity Manager Private Viewpager Pager; Viewpager@overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//Initialize Resource pager = (Viewpager) Findviewbyid (r.id.viewpager); tabhost = (tabHost) Findviewbyid (r.id.tab_host); manager = Getsupportfragmentmanager (); views = new arraylist<view> (); Views.add ( Manager.findfragmentbyid (r.id.fragment_image). GetView ()) Views.add (Manager.findfragmentbyid (R.id.fragment_text ). GetView ()); Views.add (Manager.findfragmentbyid (R.id.fragment_both). GetView ());//Management Tabhost start Tabhost.setup ();// Pass an empty content to tabhost, cannot use above two fragmenttabcontentfactory factory = new Tabcontentfactory () {@Overridepublic View Createtabcontent (String tag) {return new View (mainactivity.this);}};/ /Tab1tabspec Tabspec = Tabhost.newtabspec (page1_id); Tabspec.setindicator (Createtabview (R.string.fragment_image_ STR); tabspec.setcontent (factory); Tabhost.addtab (tabspec);//Tab2tabspec TABSPEC2 = Tabhost.newtabspec (page2_id); Tabspec2.setindicator (Createtabview (R.string.fraGMENT_TEXT_STR)); Tabspec2.setcontent (factory); Tabhost.addtab (TABSPEC2);//Tab3tabspec TABSPEC3 = Tabhost.newtabspec (page3_id); Tabspec3.setindicator (Createtabview (R.STRING.FRAGMENT_BOTH_STR)); Tabspec3.setcontent (Factory); Tabhost.addtab (TABSPEC3); tabhost.setcurrenttab (0);//Management tabhost End// Set Listener and Adapter Pager.setadapter (new PageAdapter ());p Ager.setonpagechangelistener (New Pagechangelistener ()); Tabhost.setontabchangedlistener (New Tabchangelistener ());}  /** * PageView Adapter * * @author Administrator * */private class PageAdapter extends Pageradapter {@Overridepublic int GetCount () {return views.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic void Destroyitem (viewgroup view, int position, Object arg2) {View.removeview (Views.get (position));} @Overridepublic Object Instantiateitem (viewgroup view, int position) {try {if (Views.get (position). getParent () = = null) { View.addview (Views.get (position));} else {(viewgroup) views.get (position).GetParent ()). Removeview (Views.get (position)); View.addview (Views.get (position));}} catch (Exception e) {e.printstacktrace ();} return Views.get (position);}} /** * TAB Click Toggle Listener * * @author Administrator * */private class Tabchangelistener implements Ontabchangelistener {@Override public void ontabchanged (String tabId) {if (Page1_id.equals (tabId)) {pager.setcurrentitem (0);} else if (page2_id.equals (tabId)) {Pager.setcurrentitem (1);} else if (page3_id.equals (tabId)) {Pager.setcurrentitem (2);}}} /** * Viewpager SWIPE Toggle Listener * * @author Administrator * */private class Pagechangelistener implements Onpagechangelistener {@ overridepublic void onpagescrollstatechanged (int arg0) {} @Overridepublic void onpagescrolled (int arg0, float arg1, int ar G2) {} @Overridepublic void onpageselected (int arg0) {tabhost.setcurrenttab (arg0);}} /** * Create tab View * * @param string * @return */private view createtabview (int stringid) {View Tabview = Getlayoutinflater (). Inflate (r.layout.tab, NULL); TextView TextView = (TextView) Tabview.findviewbyid (R.id.tab_text); Textview.settext (Stringid); return tabview;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {int id = item.getitemid (); if (id = = r.id.action_settings) { return true;} return super.onoptionsitemselected (item);}}

One of theLayout/tab.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical"    android:background= "@drawable/state_tabs_bg" >        <textview android:id=         "@+ Id/tab_text "        android:layout_width=" wrap_content "    android:layout_height=" Wrap_content "    android: layout_margintop= "11DP"    android:layout_gravity= "center_horizontal"    android:textsize= "18sp    " Android:textcolor= "@color/tab_widget_text"/></linearlayout>

Drawable/state_tabs_bg.xml
<?xml version= "1.0" encoding= "utf-8"?><!--tab each label background--><selector xmlns:android= "/http/ Schemas.android.com/apk/res/android ">    <item android:state_selected=" true "android:drawable=" @drawable/ Tabs_selected_bg "/> <item android:drawable=" @drawable/tabs_normal_bg "></item></selector>

Drawable/tabs_selected_bg.xml
<?xml version= "1.0" encoding= "UTF-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android >    <item>        <shape>            <solid android:color= "@color/color_orange"/>        < /shape>    </item>    <item android:bottom= "5DP" >        <shape>            <solid android:color = "#eeeeee"/>        </shape>    </item></layer-list>

Drawable/tabs_normal_bg.xml
<?xml version= "1.0" encoding= "UTF-8"? ><layer-list xmlns:android= "http://schemas.android.com/apk/res/ Android >  <item>        <shape>            <solid android:color= "@color/color_orange"/>        < /shape>    </item>    <item android:bottom= "1DP" >        <shape>            <solid android:color = "#eeeeee"/>        </shape>    </item></layer-list>

Ok so far, our code is OK, but when you run to find that can not run up, why???
And look inside our frame, modifyImageframe.java:
Package Com.jov.germany.frame;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Com.jov.germany.R; public class ImageFrame extends fragment{@Override public      View Oncreateview (layoutinflater inflater, ViewGroup Container,              Bundle savedinstancestate) {          return inflater.inflate (R.layout.image_frame, container, false);      }  }

You may feel no difference, look at our fragment quoted package, not the same OH.
All right, last.the string content in the Mainacitivity.java itself in the String.xml.

No cut-slip effect. Recently all kinds of busy, updated a little slow, no way ah
Source Here

[Android Development Content Update class app] Iv. layout of basic functions of the Project (cont.)

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.