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

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"" /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" >

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

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



And then we'll write a layout.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= ""

 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

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

Import Android.widget.TextView;

 /** * 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 ( setbackgRoundresource (R.mipmap.image_tabbar_button_home_down);
 TextView tvhome = (TextView) Homelayout.findviewbyid (;
 Tvhome.settext ("home page");

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

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

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

 Tvloaction.settextcolor (Color.White);
 Settinglayout.findviewbyid ( Setbackgroundresource (R.mipmap.image_tabbar_button_more); TeXtview tvsetting = (TextView) Settinglayout.findviewbyid (;
 Tvsetting.settext ("Settings");
 Tvsetting.settextcolor (Color.White); /** * method to find the control * @param view */private void Findview (view view) {Homelayout = (relativelayout) view.findviewb
 Yid (;
 Selectlayout = (relativelayout) View.findviewbyid (;
 Searchlayout = (relativelayout) View.findviewbyid (;
 Locationlayout = (relativelayout) View.findviewbyid (;
 Settinglayout = (relativelayout) View.findviewbyid (;
 /** * 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 (0);
  Case (1);
  Case (2);
  Case (3);
  Case (4);
 } icallbacklistener.clic (V.getid ()); }/** * Toggle card position */public void Initpix (int i) {switch (i) {case 0:homelayout.findviewbyid ( Setbac
 Kgroundresource (R.mipmap.image_tabbar_button_home_down);
 TextView TVHOME0 = (TextView) Homelayout.findviewbyid (;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

 Tvsetting4.settextcolor (Color.Blue);


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 ( Fragment) {
 Fragmentmanager manager = Getfragmentmanager (); ft = manager.begintransaction ();
 Ft.replace (,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
  initpagercontent (new Homefragment ());

  initpagercontent (New Selectfragment ());

  initpagercontent (New Searchfragment ());

  initpagercontent (New Loactionfragment ());

  initpagercontent (New Settingfragment ());



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.

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"" /res/android "
 android:layout_width=" match_parent "
 android:layout_height=" match_parent ">

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

Specifically, I'll just post the mainactivity code.

 Package com.lgl.baiduwallpaper;
Import Android.os.Bundle;

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 ( Fragment) {//Fragmentmanager Manag
ER = Getfragmentmanager (); ft = manager.begintransaction ();
Ft.replace (,fragment);
Ft.commit ();
 /** * Click event/private void Setonclick () {Mybottomlayout.setoncallbacklistener (New Mycallbacklistener ());

 /** * Find control/private void Findview () {mybottomlayout = (mybottomlayout) Findviewbyid (;
 Viewpager = (Viewpager) Findviewbyid (;
 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 (New Homefragment ());
  Viewpager.setcurrentitem (0);
  Case (New Selectfragment ());
  Viewpager.setcurrentitem (1);
  Case (New Searchfragment ());
  Viewpager.setcurrentitem (2);
  Case (New Loactionfragment ());
  Viewpager.setcurrentitem (3);
  Case (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: 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.