Android Internship notes (one)---magical include to solve layout reuse problems
--Reprint Please specify Source: Coder-pig
If you already know what the include is, just want to know how to use it, using the example below:
① Layout File Introduction layouts
<include android:id= "@+id/topbar" android:layout_width= "match_parent" android:layout_height= " Wrap_content " layout=" @layout/view_topbar "/>
Access in ②java code, get components in Layout, set properties:
Private View topbar;private TextView Txttitle;topbar = Findviewbyid (r.id.topbar); txttitle = (TextView) Topbar.findviewbyid (R.id.txttitle); Txttitle.settext ("second page");
just so simple, you can close the page, feel that help you, you can order a praise!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you do not know, want to know the relevant and application scenarios please continue to look:
This section of the flowchart:
:
Code Ideas:
The core is a titlebar at the top, and here is a bar we write ourselves, because the next two of the activity is needed,
So we're going to include this bar directly from the include, and then get the bar instance in Java code and call Bar.findviewbyid
Find the title bar of the TextView to modify the corresponding page!
The detailed code is as follows:
Bar at top: view_topbar.xml
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" Match_parent " android: background= "#000000" > <imageview android:id= "@+id/imgback" android:layout_width= "40DP " android:layout_height= "40DP" android:background= "@drawable/back"/> <textview android:id= "@+ Id/txttitle " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android: Layout_centerhorizontal= "true" android:layout_margintop= "8DP" android:textcolor= "#FFFFFF" Android:textsize= "20SP"/></relativelayout>
then you write three activity layouts, and two or three activity is imported through include Topbar
Activity_main.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:background= "#D1E5E6" tools:context= "com.jay.example.includedemo.MainActivity" > < Button android:id= "@+id/btnto" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " android:layout_centerinparent=" true " android:text=" jumps to the second page "/></relativelayout>
Activity_second.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:background= "#D5E094" > <include android:id= "@+id/topbar" android:layout_width= " Match_parent " android:layout_height=" wrap_content " layout=" @layout/view_topbar "/> <button android:id= "@+id/btnto" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content " android:layout_centerinparent=" true " android:text=" jumps to the third page " android:textsize=" 14SP "/ ></RelativeLayout>
Activity_third.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:background= "#F2CC9F" > <include android:id= "@+id/topbar" android:layout_width= " Match_parent " android:layout_height=" wrap_content " layout=" @layout/view_topbar "/> <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_ Centerinparent= "true" android:text= "third page"/></relativelayout>
Write the corresponding activity:
Mainactivity.java:
Package Com.jay.example.includedemo;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; public class Mainactivity extends Activity {private Button btnto; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); GetViews (); Setviews ();} private void Getviews () {Btnto = (Button) Findviewbyid (R.id.btnto);} private void Setviews () {Myclick Myclick = new Myclick (); Btnto.setonclicklistener (Myclick);} Define event handling Class private class Myclick implements Onclicklistener {@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btnto:intent it = new Intent (mainactivity.this, Secondactivity.class); startactivity (it); break;}}}
Secondactivity.java:
Package Com.jay.example.includedemo;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import Android.widget.imageview;import Android.widget.textview;public class Secondactivity extends Activity {private View topbar;private ImageView imgback;private TextView txttitle;private Button btnto; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_second); Getactionbar (). Hide (); Getviews (); Setviews ();} private void Getviews () {Topbar = Findviewbyid (r.id.topbar); Btnto = (Button) Findviewbyid (r.id.btnto); imgback = ( ImageView) Topbar.findviewbyid (r.id.imgback); txttitle = (TextView) Topbar.findviewbyid (r.id.txttitle);} private void Setviews () {Myclick Myclick = new Myclick (); Txttitle.settext ("second page"); Btnto.setonclicklistener (Myclick); Imgback.setonclicklistener (Myclick);} Defining event-handling classes private class Myclick IMPlements Onclicklistener {@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btnto:intent it = new Intent (Secondactivity.this, Thirdactivity.class); startactivity (it); break;case r.id.imgback:finish (); break;}}}
Thirdactivity.java:
Package Com.jay.example.includedemo;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.imageview;import Android.widget.textview;public Class Thirdactivity extends Activity {private View topbar;private ImageView imgback;private TextView txttitle;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_third); Getactionbar (). Hide (); Getviews (); Setviews ();} private void Getviews () {Topbar = Findviewbyid (r.id.topbar); imgback = (ImageView) Topbar.findviewbyid (r.id.imgback); Txttitle = (TextView) Topbar.findviewbyid (r.id.txttitle);} private void Setviews () {Myclick Myclick = new Myclick (); Txttitle.settext ("third page"); Imgback.setonclicklistener (Myclick);} Define event handling Class private class Myclick implements Onclicklistener {@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.imgback:finish (); break;}}}
Finally, the configuration file registration under the 232 activity can!
<activity android:name= ". Secondactivity "></activity> <activity android:name=". Thirdactivity "></activity>
Code Download:
Http://pan.baidu.com/s/1bnpjJhP
Android Internship notes (one)---magical include to solve layout reuse problems