Android仿百度壁纸客户端之搭建主框架(一)

Android仿百度壁纸客户端之搭建主框架(一),第1张

概述这是个不错的教程,自己学完了之后就拿出来分享了,本来想一个帖子写完,但是发现这样对自己写博客的效率有点出入,为了让大家看的舒服点,所以分开来写,我们先开看下百度壁纸客户端是什么样子的

这是个不错的教程,自己学完了之后就拿出来分享了,本来想一个帖子写完,但是发现这样对自己写博客的效率有点出入,为了让大家看的舒服点,所以分开来写,我们先开看下百度壁纸的客户端是什么样子的

我们先来写个主页的框架,我们新建一个项目――BaIDuWallPaper 

写个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>

然后我们再写个布局 

<?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_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_weight="1" /> <include androID:ID="@+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>

这样我们就可以自定义组合控件了
 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;/** * 底部布局 * Created by lgl on 16/3/31. */public class MyBottomLayout extends linearLayout { //跟布局是relativeLayout private relativeLayout homeLayout,selectLayout,searchLayout,locationLayout,settingLayout; //布局加载 private LayoutInflater inflater; //构造方法 public MyBottomLayout(Context context,AttributeSet attrs) { super(context,attrs); initVIEw(); } /** * 初始化 */ private voID initVIEw() { inflater = LayoutInflater.from(getContext()); VIEw vIEw = inflater.inflate(R.layout.layout_bottom,this); findVIEw(vIEw); initData(); setonClick(); } /** * 初始化数据 */ 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("首页"); 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("精选"); 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("搜索"); 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("本地"); 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("设置"); tvSetting.setTextcolor(color.WHITE); } /** * 找到控件的方法 * * @param vIEw */ private voID findVIEw(VIEw vIEw) { homeLayout = (relativeLayout) vIEw.findVIEwByID(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); } /** * 控件的点击事件 */ private voID setonClick() { homeLayout.setonClickListener(new Lister()); selectLayout.setonClickListener(new Lister()); searchLayout.setonClickListener(new Lister()); locationLayout.setonClickListener(new Lister()); settingLayout.setonClickListener(new Lister()); } /** * 点击接口 */ private class Lister implements OnClickListener { /** * 点击后改变点击状态 * 切换页面 * * @param v */ @OverrIDe 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()); } } /** * 切换卡的位置 */ public voID initPix(int i) { switch (i) { case 0: homeLayout.findVIEwByID(R.ID.tabimg).setBackgroundResource(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; } }}

我们运行一下

接下来我们让他可以切换选项卡,我们定义一个接口

 /** * 切换页面的接口 */ public interface ICallbackListener { public voID clic(int ID); } ICallbackListener iCallbackListener = null; public voID setonCallbackListener(ICallbackListener iCallbackListener) { this.iCallbackListener = iCallbackListener; }

接着初始化数据

 /** * 设置默认的第一页数据 */ private voID initPagerContent(androID.app.Fragment fragment) { FragmentManager manager = getFragmentManager(); androID.app.FragmentTransaction ft = manager.beginTransaction(); ft.replace(R.ID.myContent,fragment); ft.commit(); }

然后我们引用的时候就可以直接new了

 /** * 切换接口 */ 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; } } }

 我们在运行一下 

但是有一点我们要知道,我们还要实现滑动,这样的话,我们就要使用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>

具体的,我就直接把MainActivity的代码贴上吧

 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;/** * 主界面 */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(); } /** * 初始化 */ private voID initVIEw() {// initPagerContent(new HomeFragment()); findVIEw(); setonclick(); }// /**// * 设置默认的第一页数据// */// private voID initPagerContent(androID.app.Fragment fragment) {// FragmentManager manager = getFragmentManager();// androID.app.FragmentTransaction ft = manager.beginTransaction();// ft.replace(R.ID.myContent,fragment);// ft.commit();// } /** * 点击事件 */ private voID setonclick() { myBottomLayout.setonCallbackListener(new MyCallbackListener()); } /** * 找寻控件 */ private voID findVIEw() { myBottomLayout = (MyBottomLayout) findVIEwByID(R.ID.myBottomLayout); vIEwpager = (VIEwPager) findVIEwByID(R.ID.myVIEwPager); vIEwpager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager())); //页面监听 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) { } }); } /** * 切换接口 */ private class MyCallbackListener implements MyBottomLayout.ICallbackListener { @OverrIDe public voID 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 MyFragmentAdapter(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个页面 return 5; } }}

主要是你切换的时候setCurrentItem(ID);同时监听vIEwpager的滑动,就可以自由切换了,我们运行一下

源码下载: Android仿百度壁纸客户端

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android仿百度壁纸客户端之搭建主框架(一)全部内容,希望文章能够帮你解决Android仿百度壁纸客户端之搭建主框架(一)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1148708.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存