Android imitation Baidu Wallpaper Client building main frame (i) _android

Source: Internet
Author: User

This is a good tutorial, after I learned to take out to share, originally wanted to write a post, but found that the efficiency of their own blog a little different, in order to let everyone see the comfortable point, so separate to write, we first look at the Baidu wallpaper client is what it looks like

Let's start by writing a homepage framework, we create a new project--baiduwallpaper

Write an item
Layout_tab_item

<?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 ">

 < Relativelayout
 android:layout_width= "match_parent"
 android:layout_height= "Wrap_content"
 android: Layout_centerinparent= "true" >

 <imageview
 android:id= "@+id/tabimg"
 android:layout_width= " Wrap_content "
 android:layout_height=" wrap_content "
 android:layout_centerhorizontal=" true "/>

 <textview
 android:id= "@+id/tabtext"
 android:layout_width= "wrap_content"
 android:layout_ height= "Wrap_content"
 android:layout_below= "@+id/tabimg"
 android:layout_centerhorizontal= "true"
 android:text= "@string/app_name"
 android:textcolor= "@android: Color/white"
 android:textsize= " 16sp "/>


 </RelativeLayout>

</RelativeLayout>

And then we'll write a 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=" 70DP "android:orientation=" Horizontal ">
 <include android:id= "@+id/homelayout" layout= "@layout/layout_tab_item" android:layout_width= "Match_parent"
 android:layout_height= "Match_parent" android:layout_weight= "1"/> <include android:id= "@+id/selectLayout" layout= "@layout/layout_tab_item" android:layout_width= "match_parent" android:layout_height= "Match_parent" Android : layout_weight= "1"/> <include android:id= "@+id/searchlayout" layout= "@layout/layout_tab_item" android:layout "Match_parent" android:layout_height= "Match_parent" android:layout_weight= "1"/> <include android:id= "_width=" @+id/locationlayout "layout=" @layout/layout_tab_item "android:layout_width=" Match_parent "android:layout_height=" Match_parent "android:layout_weight=" 1 "/> <include androiD:id= "@+id/settinglayout" layout= "@layout/layout_tab_item" android:layout_width= "Match_parent" Android:layout_

 height= "Match_parent" android:layout_weight= "1"/> </LinearLayout>

So we can customize the combo control
 mybottomlayout

Package Com.lgl.baiduwallpaper.view;
Import Android.content.Context;
Import Android.graphics.Color;
Import Android.util.AttributeSet;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.LinearLayout;
Import Android.widget.RelativeLayout;

Import Android.widget.TextView;

Import COM.LGL.BAIDUWALLPAPER.R;
 /** * Bottom Layout * Created by LGL on 16/3/31. * * public class Mybottomlayout extends LinearLayout {//with layout is relativelayout private relativelayout homelayout, Selectlay
 Out, Searchlayout, Locationlayout, settinglayout;

 Layout loaded private Layoutinflater inflater;

 Constructs the method public Mybottomlayout (the context context, the AttributeSet attrs) {Super (context, attrs);
 Initview ();
 }/** * Initialize/private void Initview () {inflater = Layoutinflater.from (GetContext ());
 View view = Inflater.inflate (R.layout.layout_bottom, this);
 Findview (view);
 InitData ();
 Setonclick (); }/** * Initialization data/private void InitData () {Homelayout.findviewbyid (r.id.tabimg). setbackgRoundresource (R.mipmap.image_tabbar_button_home_down);
 TextView tvhome = (TextView) Homelayout.findviewbyid (R.id.tabtext);
 Tvhome.settext ("home page");

 Tvhome.settextcolor (Color.Blue);
 Selectlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_search);
 TextView Tvselect = (TextView) Selectlayout.findviewbyid (R.id.tabtext);
 Tvselect.settext ("selected");

 Tvselect.settextcolor (Color.White);
 Searchlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_find);
 TextView Tvsearch = (TextView) Searchlayout.findviewbyid (R.id.tabtext);
 Tvsearch.settext ("search");

 Tvsearch.settextcolor (Color.White);
 Locationlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_manage);
 TextView tvloaction = (TextView) Locationlayout.findviewbyid (R.id.tabtext);
 Tvloaction.settext ("local");

 Tvloaction.settextcolor (Color.White);
 Settinglayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_more); TeXtview tvsetting = (TextView) Settinglayout.findviewbyid (R.id.tabtext);
 Tvsetting.settext ("Settings");
 Tvsetting.settextcolor (Color.White); /** * method to find the control * @param view */private void Findview (view view) {Homelayout = (relativelayout) view.findviewb
 Yid (r.id.homelayout);
 Selectlayout = (relativelayout) View.findviewbyid (r.id.selectlayout);
 Searchlayout = (relativelayout) View.findviewbyid (r.id.searchlayout);
 Locationlayout = (relativelayout) View.findviewbyid (r.id.locationlayout);
 Settinglayout = (relativelayout) View.findviewbyid (r.id.settinglayout);
 /** * Control Click event/private void Setonclick () {Homelayout.setonclicklistener (New Lister ());
 Selectlayout.setonclicklistener (New Lister ());
 Searchlayout.setonclicklistener (New Lister ());
 Locationlayout.setonclicklistener (New Lister ());
 Settinglayout.setonclicklistener (New Lister ()); /** * Click on the interface/private class Lister implements Onclicklistener {/** * Click to change the click state * Switch Page * * @param v * * * @Ove Rride Public void OnClick (View v) {switch (V.getid ()) {case R.id.homelayout:initpix (0);
 Break
  Case R.id.selectlayout:initpix (1);
 Break
  Case R.id.searchlayout:initpix (2);
 Break
  Case R.id.locationlayout:initpix (3);
 Break
  Case R.id.settinglayout:initpix (4);
 Break
 } icallbacklistener.clic (V.getid ()); }/** * Toggle card position */public void Initpix (int i) {switch (i) {case 0:homelayout.findviewbyid (r.id.tabimg). Setbac
 Kgroundresource (R.mipmap.image_tabbar_button_home_down);
 TextView TVHOME0 = (TextView) Homelayout.findviewbyid (R.id.tabtext);

 Tvhome0.settextcolor (Color.Blue);
 Selectlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_search);
 TextView tvSelect0 = (TextView) Selectlayout.findviewbyid (R.id.tabtext);

 Tvselect0.settextcolor (Color.White);
 Searchlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_find);
 TextView tvSearch0 = (TextView) Searchlayout.findviewbyid (R.id.tabtext); TVsearch0.settextcolor (Color.White);
 Locationlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_manage);
 TextView TvLocation0 = (TextView) Locationlayout.findviewbyid (R.id.tabtext);

 Tvlocation0.settextcolor (Color.White);
 Settinglayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_more);
 TextView TVSETTING0 = (TextView) Settinglayout.findviewbyid (R.id.tabtext);

 Tvsetting0.settextcolor (Color.White);
 Break
 Case 1:homelayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_home);
 TextView tvHome1 = (TextView) Homelayout.findviewbyid (R.id.tabtext);

 Tvhome1.settextcolor (Color.White);
 Selectlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_search_down);
 TextView TvSelect1 = (TextView) Selectlayout.findviewbyid (R.id.tabtext);

 Tvselect1.settextcolor (Color.Blue); Searchlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_buttoN_find);
 TextView tvSearch1 = (TextView) Searchlayout.findviewbyid (R.id.tabtext);

 Tvsearch1.settextcolor (Color.White);
 Locationlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_manage);
 TextView TvLocation1 = (TextView) Locationlayout.findviewbyid (R.id.tabtext);

 Tvlocation1.settextcolor (Color.White);
 Settinglayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_more);
 TextView tvSetting1 = (TextView) Settinglayout.findviewbyid (R.id.tabtext);

 Tvsetting1.settextcolor (Color.White);
 Break
 Case 2:homelayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_home);
 TextView tvHome2 = (TextView) Homelayout.findviewbyid (R.id.tabtext);

 Tvhome2.settextcolor (Color.White);
 Selectlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_search);
 TextView tvSelect2 = (TextView) Selectlayout.findviewbyid (R.id.tabtext);

 Tvselect2.settextcolor (Color.White); SearchlAyout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_find_down);
 TextView TVSEARCH2 = (TextView) Searchlayout.findviewbyid (R.id.tabtext);

 Tvsearch2.settextcolor (Color.Blue);
 Locationlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_manage);
 TextView TvLocation2 = (TextView) Locationlayout.findviewbyid (R.id.tabtext);

 Tvlocation2.settextcolor (Color.White);
 Settinglayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_more);
 TextView tvSetting2 = (TextView) Settinglayout.findviewbyid (R.id.tabtext);


 Tvsetting2.settextcolor (Color.White);
 Break
 Case 3:homelayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_home);
 TextView TvHome3 = (TextView) Homelayout.findviewbyid (R.id.tabtext);

 Tvhome3.settextcolor (Color.White);
 Selectlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_search); TextView tvSelect3 = (TextView) seLectlayout.findviewbyid (R.id.tabtext);

 Tvselect3.settextcolor (Color.White);
 Searchlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_find);
 TextView TvSearch3 = (TextView) Searchlayout.findviewbyid (R.id.tabtext);

 Tvsearch3.settextcolor (Color.White);
 Locationlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_manage_down);
 TextView TvLocation3 = (TextView) Locationlayout.findviewbyid (R.id.tabtext);

 Tvlocation3.settextcolor (Color.Blue);
 Settinglayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_more);
 TextView tvSetting3 = (TextView) Settinglayout.findviewbyid (R.id.tabtext);

 Tvsetting3.settextcolor (Color.White);
 Break
 Case 4:homelayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_home);
 TextView tvHome4 = (TextView) Homelayout.findviewbyid (R.id.tabtext);

 Tvhome4.settextcolor (Color.White); Selectlayout.findviewbyid (r.id.tabimg). SetbackgroUndresource (R.mipmap.image_tabbar_button_search);
 TextView TvSelect4 = (TextView) Selectlayout.findviewbyid (R.id.tabtext);

 Tvselect4.settextcolor (Color.White);
 Searchlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_find);
 TextView TVSEARCH4 = (TextView) Searchlayout.findviewbyid (R.id.tabtext);

 Tvsearch4.settextcolor (Color.White);
 Locationlayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_manage);
 TextView TvLocation4 = (TextView) Locationlayout.findviewbyid (R.id.tabtext);

 Tvlocation4.settextcolor (Color.White);
 Settinglayout.findviewbyid (r.id.tabimg). Setbackgroundresource (R.mipmap.image_tabbar_button_more_down);
 TextView TvSetting4 = (TextView) Settinglayout.findviewbyid (R.id.tabtext);

 Tvsetting4.settextcolor (Color.Blue);
 Break

 }
 }
}

Let's run a little bit.

Next we get him to switch tabs, we define an interface

 /**
 * Toggle the interface of the page * * Public
 interface Icallbacklistener {public
 void clic (int id);
 }

 Icallbacklistener icallbacklistener = null;

 public void Setoncallbacklistener (Icallbacklistener icallbacklistener) {
 This.icallbacklistener = Icallbacklistener;
 }

Then initialize the data

 /**
 * Set the default first page data * *
 private void Initpagercontent (Android.app.Fragment Fragment) {
 Fragmentmanager manager = Getfragmentmanager ();
 Android.app.FragmentTransaction ft = manager.begintransaction ();
 Ft.replace (r.id.mycontent,fragment);
 Ft.commit ();
 }

And then we're going to be able to direct new when we quote.

 /**
 * Switch Interface * *
 Private class Mycallbacklistener implements Mybottomlayout.icallbacklistener {

 @ Override public
 void clic (int id) {
 switch (ID) {case
 r.id.homelayout:
  initpagercontent (new Homefragment ());

  break;
 Case R.id.selectlayout:
  initpagercontent (New Selectfragment ());

  break;
 Case R.id.searchlayout:
  initpagercontent (New Searchfragment ());

  break;
 Case R.id.locationlayout:
  initpagercontent (New Loactionfragment ());

  break;
 Case R.id.settinglayout:
  initpagercontent (New Settingfragment ());

  break;

 }}}
 

We're running a little bit.

But one thing we need to know is that we're going to be sliding, so we're going to use Viewpager.
Layout_main.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.support.v4.view.ViewPager
 android:id= "@+id/myviewpager"
 android:layout_width= "Match_parent"
 android:layout_height= "match_parent"
 android:layout_above= "@+id/mybottomlayout"/>

 < Com.lgl.baiduwallpaper.view.MyBottomLayout
 android:id= "@+id/mybottomlayout"
 android:layout_width= " Match_parent "
 android:layout_height=" wrap_content "
 android:layout_alignparentbottom=" true "
 android:background= "@mipmap/image_titlebar_background"/>
</RelativeLayout>

Specifically, I'll just post the mainactivity code.

 Package com.lgl.baiduwallpaper;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import android.support.v4.app.FragmentActivity;
Import Android.support.v4.app.FragmentManager;
Import Android.support.v4.app.FragmentPagerAdapter;

Import Android.support.v4.view.ViewPager;
Import com.lgl.baiduwallpaper.fragment.HomeFragment;
Import com.lgl.baiduwallpaper.fragment.LoactionFragment;
Import com.lgl.baiduwallpaper.fragment.SearchFragment;
Import com.lgl.baiduwallpaper.fragment.SelectFragment;
Import com.lgl.baiduwallpaper.fragment.SettingFragment;

Import Com.lgl.baiduwallpaper.view.MyBottomLayout;
 /** * Main Interface */public class Mainactivity extends Fragmentactivity {private mybottomlayout mybottomlayout;

 Private Viewpager Viewpager;
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

 Setcontentview (R.layout.activity_main);
 Initview (); }/** * Initialize/private void Initview () {//Initpagercontent (New Homefragment ());
 Findview ();
 Setonclick (); ////**//* Set default first page data////private void Initpagercontent (Android.app.Fragment Fragment) {//Fragmentmanager Manag
ER = Getfragmentmanager ();
Android.app.FragmentTransaction ft = manager.begintransaction ();
Ft.replace (r.id.mycontent,fragment);
Ft.commit ();
 /** * Click event/private void Setonclick () {Mybottomlayout.setoncallbacklistener (New Mycallbacklistener ());

 /** * Find control/private void Findview () {mybottomlayout = (mybottomlayout) Findviewbyid (r.id.mybottomlayout);
 Viewpager = (Viewpager) Findviewbyid (R.id.myviewpager);
 Viewpager.setadapter (New Myfragmentadapter (Getsupportfragmentmanager ())); Page Listener Viewpager.setonpagechangelistener (new Viewpager.onpagechangelistener () {@Override public void onpagescrolled ( int position, float positionoffset, int positionoffsetpixels) {} @Override public void onpageselected (int position)
 {Mybottomlayout.initpix (position); } @Override public void OnpagescrollstatecHanged (int state) {}}); /** * Switch Interface * * Private class Mycallbacklistener implements Mybottomlayout.icallbacklistener {@Override public vo
  ID clic (int id) {switch (ID) {case r.id.homelayout://Initpagercontent (New Homefragment ());
  Viewpager.setcurrentitem (0);
 Break
  Case R.id.selectlayout://Initpagercontent (New Selectfragment ());
  Viewpager.setcurrentitem (1);
 Break
  Case R.id.searchlayout://Initpagercontent (New Searchfragment ());
  Viewpager.setcurrentitem (2);
 Break
  Case R.id.locationlayout://Initpagercontent (New Loactionfragment ());
  Viewpager.setcurrentitem (3);
 Break
  Case R.id.settinglayout://Initpagercontent (New Settingfragment ());
  Viewpager.setcurrentitem (4);

 Break /** * Viewpager Adapter/Private class Myfragmentadapter extends Fragmentpageradapter {public myfragment
 Adapter (Fragmentmanager FM) {super (FM); @Override public Fragment getitem (int position) {switch (position) {Case 0:return new HomEfragment ();
 Case 1:return New Selectfragment ();
 Case 2:return New Searchfragment ();
 Case 3:return New Loactionfragment ();
 Case 4:return New Settingfragment ();
 return null;
 @Override public int GetCount () {//5 pages return 5;
 }
 }
}

The main is when you switch Setcurrentitem (ID), while listening to Viewpager sliding, you can freely switch, we run a

SOURCE download: Android imitation Baidu wallpaper Client

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.