Recently doing some Android simple layout application, the following is the effect I do this time, I feel very beautiful to take out for you to share
First of all, to define four findworkfragment,findtalentsfragment,findschoolfragment,findstudentfragment classes note that the inheritance is Fragment Class (Android.support.v4.app.Fragment) because I do the first class of specific functions, so also take Findworkfragment class in the show it
1. Findworkfragment layout
<?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:orientation=" vertical " android:background= "#31C4F7" > <!---up clearance--<view android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "ten"/> <!--title---<textview an Droid:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "Android:text" = "Job Search" android:textcolor= "#fff" android:textsize= "40SP" android:gravity= "center"/> <!-- Clearance--<view android:layout_width= "match_parent" android:layout_height= "0DP" android:layout _weight= "/>" <!--name--<linearlayout android:layout_width= "Match_parent" Android : layout_height= "0DP" Android: layout_weight= "android:orientation=" Horizontal > <!--name-to-<TEXTVI EW android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight = "android:text=" Name: "android:textcolor=" #fff "android:textsize=" 20sp "Android oid:gravity= "Right|center_vertical"/> <!--Clearance--<view android:layout_width= "0d P "android:layout_height=" Match_parent "android:layout_weight=" 2 "/> <!--input box--< EditText android:id= "@+id/etname" android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "android:hint=" Please enter name "android:background=" #fff "Andro Id:gravity= "left|center_vertical" android:paddingleft= "10DP" android:textsize= "23sp"/> < ;! --Clearance--< View android:layout_width= "0DP" android:layout_height= "match_parent" android:layout_weight= "one"/> </LinearLayout> <!--Clearance--<view android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "/>" <!--mobile phone number---<linearlayout Android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "Android:or" ientation= "Horizontal" > <!--name---<textview android:layout_width= " 0DP "android:layout_height=" match_parent "android:layout_weight=" "android:text=" Phone Number: " Android:textcolor= "#fff" android:textsize= "20sp" android:gravity= "right|center_vertical"/ > <!--Clearance--<view android:layout_width= "0DP" android:layout_height= "mat Ch_parent "Androidoid:layout_weight= "2"/> <!--input Box--<edittext android:id= "@+id/etphone" Android:layo Ut_width= "0DP" android:layout_height= "match_parent" android:layout_weight= "Android:h" int= "Please enter phone number" android:background= "#fff" android:gravity= "left|center_vertical" Android:pad dingleft= "10DP" android:textsize= "23sp"/> <!--clearance and <view Android:layout_wi Dth= "0DP" android:layout_height= "match_parent" android:layout_weight= "one"/> </linearlayout& Gt <!--clearance--<view android:layout_width= "match_parent" android:layout_height= "0DP" Androi d:layout_weight=/> <!--Select Jobs--<linearlayout android:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "android:orientation=" "Horizontal" > <!--name--<textview android:layout_width= "0DP" android:layout_height= "Match_parent" Android:layout_weight= "android:text=" Selected jobs: "android:textcolor=" #fff "Androi D:textsize= "20SP" android:gravity= "right|center_vertical"/> <!--clearance--<view Android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "2"/> <!--Select Jobs--<spinner android:id= "@+id/sptype" android:layout_width= "0DP" Androi d:layout_weight= "102" android:layout_height= "Match_parent" android:paddingleft= "10DP" and Roid:textsize= "23SP" android:prompt= "@string/select" > </Spinner> <!--Clearance --<view android:layout_width= "0DP" android:layout_height= "Match_parent" android:layou t_weight= "Ten"/> </LinearLayout> <!--Clearance--<view android:layout_width= "Match_parent" Androi d:layout_height= "0DP" android:layout_weight=/> <!--Release--<linearlayout android: Layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "Android:orientatio" n= "Horizontal" > <!--Clearance--<view android:layout_width= "0DP" Android:layo ut_height= "Match_parent" android:layout_weight= "1"/> <!--released--<button android: Id= "@+id/btncommit" android:layout_width= "0DP" android:layout_weight= "4" Android:layout_h eight= "Match_parent" android:text= "release" android:textcolor= "#fff" android:textsize= "20SP" android:background= "#E4BA3F"/> <!--Clearance--<view android:layout_width= "0DP" Android:layout_height= "Match_parent" android:layout_weight= "1"/> <!--Reset--<button android:id= "@+i D/btnreset "android:layout_width=" 0DP "android:layout_weight=" 4 "android:layout_height=" M Atch_parent "android:text=" reset "android:textcolor=" #fff "android:textsize=" 20SP " android:background= "#E4BA3F"/> <!--clearance-<view android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1"/> </LinearLayout> &l t;! --bottom clearance--<view android:layout_width= "match_parent" android:layout_height= "0DP" Android:la yout_weight= "/></linearlayout>"
2, the specific operation of the Findworkfragment class, the inside said very detailed, I do not explain
<pre name= "code" class= "java" >package com.example.recruitget;import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.arrayadapter;import Android.widget.button;import Android.widget.edittext;import Android.widget.spinner;import android.widget.Toast;/* * Job Search * @author Fengkun * */public class Findworkfragment extends Fragment {/** find work */private View m_vfindworkfragment;/ * * Name */private EditText m_etname;/** phone */private EditText m_etphone;/** release */private Button m_btncommit;/** Reset */private Button m_btnreset;/** drop-down list */private Spinner m_sptype;private arrayadapter<string> adapter;/** option */private String [] types = new string[] {"Doctor", "worker", "teacher", "Sales", "service"};p ublic void OnCreate (Bundle savedinstancestate) {//TODO Auto-generated Method Stubsuper.oncreate (saveDinstancestate);} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {m_ Vfindworkfragment = Inflater.inflate (R.layout.find_work, container,false);//Initialize variable initui (); return m_ Vfindworkfragment;} /** * Initialize variable */private void Initui () {//Name m_etname = (EditText) M_vfindworkfragment.findviewbyid (r.id.etname);//Telephone m_etph one = (EditText) M_vfindworkfragment.findviewbyid (r.id.etphone);//Publish M_btncommit = (Button) m_ Vfindworkfragment.findviewbyid (R.id.btncommit); M_btncommit.setonclicklistener (commit);//Reset M_btnreset = (Button) m _vfindworkfragment.findviewbyid (R.id.btnreset); M_btnreset.setonclicklistener (reset);//drop-down list M_sptype = (Spinner) m_ Vfindworkfragment.findviewbyid (R.id.sptype);//define Adapter adapter = new Arrayadapter<string> (Getactivity (), Android . R.layout.simple_spinner_item, types);//Set the style of the drop-down list Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item); Add adapter to spinner M_sptype.setadapter (ADAPter); Add Event Spinner Event Listener M_sptype.setonitemselectedlistener (New Itemselectedlistener ()); M_sptype.setvisibility (view.visible); M_sptype.setselection (0, true);} /** * Spinner */private class Itemselectedlistener implements onitemselectedlistener{@Overridepublic void Onitemselected (adapterview<?> parent, View view,int position, long id) {} @Overridepublic void onnothingselected ( Adapterview<?> parent) {//TODO auto-generated Method stub}}/** * Publish event */private View.onclicklistener commit = new V Iew. Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubtoast.maketext (Getactivity () , "published Successfully! Please wait quietly ... ", Toast.length_short). Show ();}};/ * * Reset event */private View.onclicklistener reset = new View.onclicklistener () {@Overridepublic void OnClick (View v) {M_etnam E.settext (""); M_etphone.settext (""); m_sptype.setselection (0, true);};}
3, the last is the homepage operation, the first is the mainactivity layout
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:id=" @+id/linearlayout1 "android:layout_width=" Match_parent "Android:layout_hei ght= "match_parent" android:orientation= "vertical" tools:context= ". Mainactivity "android:background=" #fff > <!--middle Content-<fragment android:id= "@+id/fragmentpager" Android:name= "Com.example.recruitget.FindWorkFragment" android:layout_width= "Match_parent" android:layou t_height= "0DP" android:layout_weight= "677"/> <!--bottom navigation bar--<linearlayout Android Oid:layout_width= "Match_parent" android:layout_height= "0DP" android:layout_weight= "Android:orient" ation= "Horizontal" > <!--looking for work--<textview android:id= "@+id/tvwork" Android: Layout_width= "0DP" android:layout_weight= "android:layout_height=" Match_Parent "android:text=" Job search "android:gravity=" center "android:textcolor=" #000 "a ndroid:background= "#E4BA3F"/> <!--split Line--<view android:layout_width= "0DP" android:layout_weight= "0.5" android:layout_height= "match_parent" android:background= "#cccccc"/> ; <!--recruit Talent--<textview android:id= "@+id/tvtalents" android:layout_width= "0DP" Androi d:layout_weight= "" android:layout_height= "match_parent" android:text= "Recruit Talents" Android:text Color= "#E4BA3F" android:gravity= "center"/> <!--split Line--<view Android:lay Out_width= "0DP" android:layout_weight= "0.5" android:layout_height= "Match_parent" Android: background= "#ccc"/> <!--Find School--<textview android:id= "@+id/tvschool" Android:layout_ Width= "0DP "android:layout_weight=" android:layout_height= "match_parent" android:text= "Find a school" android:gravity= "center" android:textcolor= "#E4BA3F"/> <!--split Line--<view Android:layout_width= "0DP" android:layout_weight= "0.5" android:layout_height= "Match_paren T "android:background=" #ccc "/> <!--recruit students--<textview android:id=" @+id/tvstudent " Android:layout_width= "0DP" android:layout_weight= "android:layout_height=" Match_parent " android:text= "Recruit students" android:gravity= "center" android:textcolor= "#E4BA3F"/> </linea Rlayout></linearlayout>
4, the Mainactivity class inherits the Fragmentactivity, the concrete operation is as follows
Package Com.example.recruitget;import Android. R.color;import Android.os.bundle;import Android.app.activity;import Android.app.fragment;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmentmanager;import Android.support.v4.app.fragmenttransaction;import Android.view.menu;import Android.view.view;import Android.widget.framelayout;import android.widget.textview;/** * Features Home * @author Fengkun * */public class MainActivity Exte NDS fragmentactivity {/** Find job */private TextView tv_findwork;/** recruit Talent */private TextView tv_findtalents;/** Find School */private T Extview tv_findschool;/** Recruit Students */private TextView tv_findstudent;/** specific content */private android.support.v4.app.Fragment contentfragment;/** Management fragment */private Fragmentmanager fragmentmanager;protected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Initialize Data Initui ();} /** * Initialize variable */private void Initui () {//find work Tv_findwork = (TextView) Findviewbyid (r.id.tvwoRK); Tv_findwork.setonclicklistener (ItemClick);//Recruit Talent tv_findtalents = (TextView) Findviewbyid (r.id.tvtalents); tv_ Findtalents.setonclicklistener (ItemClick);//Find School Tv_findschool = (TextView) Findviewbyid (r.id.tvschool); tv_ Findschool.setonclicklistener (ItemClick);//Recruit Students tv_findstudent = (TextView) Findviewbyid (r.id.tvstudent); tv_ Findstudent.setonclicklistener (ItemClick);//Fragment Manager Fragmentmanager = Getsupportfragmentmanager ();} /** * Click Options */private view.onclicklistener itemclick = new View.onclicklistener () {@Overridepublic void OnClick (View v) {TV _findwork.setbackgroundcolor (0xFFFFFFFF); Tv_findwork.settextcolor (0xffe4ba3f); tv_ Findtalents.setbackgroundcolor (0XFFFFFFFF); Tv_findtalents.settextcolor (0xffe4ba3f); tv_ Findschool.setbackgroundcolor (0xFFFFFFFF); Tv_findschool.settextcolor (0xffe4ba3f); tv_ Findstudent.setbackgroundcolor (0xFFFFFFFF); Tv_findstudent.settextcolor (0xffe4ba3f); TextView TextView = (TextView) v; Textview.setbackgroundcolor (0xffe4ba3f); Textview.settextcolor (0xff000000); Open a fragment transaction fragmenttransaction transaction = fragmentmanager.begintransaction (); switch (V.getid ()) {//Find work case R.id.tvwork:contentfragment = new Findworkfragment (); Transaction.replace (R.id.fragmentpager, contentFragment); break;//recruit Talent Case r.id.tvtalents:contentfragment = new Findtalentsfragment (); Transaction.replace (R.id.fragmentPager, contentfragment); break;//Find School case r.id.tvschool:contentfragment = new Findschoolfragment (); Transaction.replace ( R.id.fragmentpager, contentfragment); break;//recruit Student Case r.id.tvstudent:contentfragment = new findstudentfragment (); Transaction.replace (R.id.fragmentpager, contentfragment); break;default:break;} Transaction.commit ();}};}
In fact, fragment is very convenient to use, and his compatibility is very good, I hope that beginners can learn!!!
Simple use of fragment (toggle interface)-fengkun