关于TabLayout的使用 ,自定义了一个框架。。。 以后写底部菜单就可以直接作为依赖库 ,不用麻烦了

首先,简单的说一下,这个底部框架的实现步骤。

1,自定义一个类,继承LinearLayout就好了 ,因为我喜欢用LinearLayout, 当然你也可以继承RelativeLayout。

2,重写它两个带参数的构造方法。

3,进入内容的编写,布局文件的处理,数据的处理。

4,给底部容器添加view,底部菜单实现的一个页面切换。

好了,由于个人的表诉能力 。直接上代码好了

package com.weight.tabbottomlib.view;

import java.util.ArrayList;

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.TextView;

/**
* 底部切换框架
*
* @author Administrator
*
*/
public class MyBottomLayout extends LinearLayout {
static ArrayList<Item> itemData = new ArrayList<Item>();
static ArrayList<View> viewArryItem = new ArrayList<View>();
LayoutInflater inflater; // 布局管理器
View mainView; // 这个是底部的容器
View viewItem;
int itemSize = 0;

public MyBottomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

/**
* 给底部的容器添加view
*
* @param view
*/
public void addBottomLayoutValue(ArrayList<Item> itemData) {
LayoutInflater inflater = LayoutInflater.from(getContext());
this.itemData = itemData;
this.itemSize = itemData.size();
for (int i = 0; i < itemData.size(); i++) {
View viewItem = inflater.inflate(R.layout.layout_tab_item, null);
viewItem.findViewById(R.id.tabImg).setBackgroundResource(
itemData.get(i).getDrawableNormalId());
TextView textView = ((TextView) (viewItem
.findViewById(R.id.tabText)));
textView.setText(itemData.get(i).getName());
textView.setTextColor(Color.WHITE);
this.addView(viewItem, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1.0f));
viewItem.setId(i);
viewArryItem.add(viewItem);
viewItem.setOnClickListener(new lister());
}
// 处理一下整理的数据
changeDataState();
}

/**
* 改变一下那个数据的状态
*/
private void changeDataState() {
if (itemSize != 5) { // 不需要处理
for (int i = itemSize; i < 5; i++) {
itemData.add(new Item());
LayoutInflater inflater = LayoutInflater.from(getContext());
View viewItem = inflater
.inflate(R.layout.layout_tab_item, null);
viewArryItem.add(viewItem);
}
}
}

public interface ICallbackLister {
public void click(int id);
}

ICallbackLister callbackLister = null;

public void setOnCallbackLister(ICallbackLister callbackLister) {
this.callbackLister = callbackLister;
}

/**
*
* @ClassName: lister
* @Description:tab点击事件的处理
* @author lumin
* @date 2015-10-25 下午12:53:36
*
*/
private class lister implements OnClickListener {
@Override
public void onClick(View clickView) {
callbackLister.click(clickView.getId());
initBottom(clickView.getId());
}

}

public static void initBottom(int id) {
switch (id) {
case 0:
// 第二步:实现页面的一个切换
initData(itemData.get(0).getDrawableSelectId(), itemData.get(1)
.getDrawableNormalId(), itemData.get(2)
.getDrawableNormalId(), itemData.get(3)
.getDrawableNormalId(), itemData.get(4)
.getDrawableNormalId());
changeTextColor(
(TextView) (viewArryItem.get(0).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(1).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(2).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(3).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(4).findViewById(R.id.tabText)));
break;
case 1:
initData(itemData.get(0).getDrawableNormalId(), itemData.get(1)
.getDrawableSelectId(), itemData.get(2)
.getDrawableNormalId(), itemData.get(3)
.getDrawableNormalId(), itemData.get(4)
.getDrawableNormalId());
changeTextColor(
(TextView) (viewArryItem.get(1).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(0).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(2).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(3).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(4).findViewById(R.id.tabText)));
break;
case 2:
initData(itemData.get(0).getDrawableNormalId(), itemData.get(1)
.getDrawableNormalId(), itemData.get(2)
.getDrawableSelectId(), itemData.get(3)
.getDrawableNormalId(), itemData.get(4)
.getDrawableNormalId());
changeTextColor(
(TextView) (viewArryItem.get(2).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(0).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(1).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(3).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(4).findViewById(R.id.tabText)));
break;
case 3:
initData(itemData.get(0).getDrawableNormalId(), itemData.get(1)
.getDrawableNormalId(), itemData.get(2)
.getDrawableNormalId(), itemData.get(3)
.getDrawableSelectId(), itemData.get(4)
.getDrawableNormalId());
changeTextColor(
(TextView) (viewArryItem.get(3).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(0).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(1).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(2).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(4).findViewById(R.id.tabText)));
break;
case 4:
initData(itemData.get(0).getDrawableNormalId(), itemData.get(1)
.getDrawableNormalId(), itemData.get(2)
.getDrawableNormalId(), itemData.get(3)
.getDrawableNormalId(), itemData.get(4)
.getDrawableSelectId());
changeTextColor(
(TextView) (viewArryItem.get(4).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(0).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(1).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(2).findViewById(R.id.tabText)),
(TextView) (viewArryItem.get(3).findViewById(R.id.tabText)));
break;
}
}

/**
*
* @param resIdOne
* @param resIdTwo
* @param resIdThree
* @param resIdFour
* @param resIdFive
*/
public static void initData(int resIdOne, int resIdTwo, int resIdThree,
int resIdFour, int resIdFive) {
try {
viewArryItem.get(0).findViewById(R.id.tabImg)
.setBackgroundResource(resIdOne);
viewArryItem.get(1).findViewById(R.id.tabImg)
.setBackgroundResource(resIdTwo);
viewArryItem.get(2).findViewById(R.id.tabImg)
.setBackgroundResource(resIdThree);
viewArryItem.get(3).findViewById(R.id.tabImg)
.setBackgroundResource(resIdFour);
viewArryItem.get(4).findViewById(R.id.tabImg)
.setBackgroundResource(resIdFive);
} catch (Exception err) {
err.printStackTrace();
// 报错的时候说明已经没有那么多的item了
}
}

/**
*
* @param txtOne
* 这个就是默认的需要改变的图片的颜色
* @param txtTwo
* @param txtThree
* @param txtFour
* @param txtFive
*/
public static void changeTextColor(TextView txtOne, TextView txtTwo,
TextView txtThree, TextView txtFour, TextView txtFive) {
txtOne.setTextColor(Color.MAGENTA);
txtTwo.setTextColor(Color.BLUE);
txtThree.setTextColor(Color.BLUE);
txtFour.setTextColor(Color.BLUE);
txtFive.setTextColor(Color.BLUE);
}
}

好了 ,这样就完成了框架的编写, 缺少了什么昵 。。。

下面就给出当前页面的item  。。 和实体。。

首先是布局文件

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

<RelativeLayout
android:layout_width="wrap_content"
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:textColor="#ffffff"
android:textSize="16dp" />
</RelativeLayout>

</RelativeLayout>

接着是。。实体文件

package com.weight.tabbottomlib.view;

public class Item {
private int drawableNormalId;
private int drawableSelectId;
private String name;
public Item(int drawableNormalId, int drawableSelectId, String name) {
super();
this.drawableNormalId = drawableNormalId;
this.drawableSelectId = drawableSelectId;
this.name = name;
}
public Item() {
super();
}
public int getDrawableNormalId() {
return drawableNormalId;
}
public void setDrawableNormalId(int drawableNormalId) {
this.drawableNormalId = drawableNormalId;
}
public int getDrawableSelectId() {
return drawableSelectId;
}
public void setDrawableSelectId(int drawableSelectId) {
this.drawableSelectId = drawableSelectId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

这样就全部实现了。使用的话 要作为一个库的哦,右键你的工程  点击Build Path 下的Config Build Path,进入Android,选择isLibrary  点击ok,就完成了。

然后具体的使用方法,接下来的文章里 我会告诉大家的。

时间: 2024-08-07 01:57:19

关于TabLayout的使用 ,自定义了一个框架。。。 以后写底部菜单就可以直接作为依赖库 ,不用麻烦了的相关文章

一个自定义MVP .net框架 AngleFrame

摘要:本篇是本人在完成.net平台下一个项目时,对于MVP框架引发的一些思考,以及开发了一个小型的配置型框架,名字叫作AngleFrame.这个项目属于前端桌面管理系统的一部分,最终要集成进去. 关键词:      .net,Framwork,MVP,框架,c# 前提:       当前有很多成熟的.net Plugin Framework,如MEF.SCSF.Sharpdevelop和OSGi.net等,它们在功能上各有特色. 需求:         1.开发的项目或者说模块属于一个大系统的一

030.自定义的MVC框架

.LOG自定义的MVC框架 M: Model模型V: View 视图C: Controller 控制器 ASP.Net 1. Web窗体编程2. Mvc编程 IHttpHandler任何处理web请求的类必须实现此接口void ProcessRequest(HttpContext context)bool IsReuseable{get;} 使用IHttpHandler实现登录与注册1.新建UserHandelr:IHttpHandler接口2.web.config配置,处理 /user.do请

【转载】如何写一个框架:步骤(下)

说明:写本文的时候作者完全是把脑子里的东西写了出来,没有参考任何的资料,所以对于每一项内容可能都是不完整的,不能作为一个完整的参考.有一些方法学的东西每个人都有自己的喜好,没有觉得的对和错. 单元测试 在这之前我们写的框架只能说是一个在最基本的情况下可以使用的框架,作为一个框架我们无法预测开发人员将来会怎么使用它,所以我们需要做大量的工作来确保框架不但各种功能都是正确的,而且还是健壮的.写应用系统的代码,大多数项目是不会去写单元测试的,原因很多: 项目赶时间,连做一些输入验证都没时间搞,哪里有时

XML序列化与反序列化+自定义XML注解框架XmlUtils

背景 前面一篇总结了Serializable的序列化与反序列化,现在接着总结XML.主要内容:XML基本的序列化与反序列化方法.一些注意事项.以及自定义了一个XML注解框架(简洁代码,解放双手). XML的序列化与反序列化 先与Serializable进行简单的对比: Serializable存储的文件,打开后无法正常查看,安全性高.xml文件可通过文本编辑器查看与编辑,可读性高(浏览器会格式化xml文件,更方便查看),安全性低: Serializable文件通过了签名,只能在自己的程序中反序列

写一个框架的详细步骤

定位 所谓定位就是回答几个问题,我出于什么目的要写一个框架,我的这个框架是干什么的,有什么特性适用于什么场景,我的这个框架的用户对象是谁,他们会怎么使用,框架由谁维护将来怎么发展等等. 如果你打算写框架,那么肯定心里已经有一个初步的定位,比如它是一个缓存框架.Web MVC框架.IOC框架.ORM/数据访问框架.RPC框架或是一个用于Web开发的全栈式框架. 是 否要重复造轮子?除非是练手项目,一般我们是有了解决不了问题的时候才会考虑不使用既有的成熟的框架而重复造轮子的,这个时候需要列出新框架主

如何写一个框架

定位 所谓定位就是回答几个问题,我出于什么目的要写一个框架,我的这个框架是干什么的,有什么特性适用于什么场景,我的这个框架的用户对象是谁,他们会怎么使用,框架由谁维护将来怎么发展等等. 如果你打算写框架,那么肯定心里已经有一个初步的定位,比如它是一个缓存框架.Web MVC框架.IOC框架.ORM/数据访问框架.RPC框架或是一个用于Web开发的全栈式框架. 是否要重复造轮子?除非是练手项目,一般我们是有了解决不了问题的时候才会考虑不使用既有的成熟的框架而重复造轮子的,这个时候需要列出新框架主要

关于如何学习一个框架的经验总结

1.怎么学习一个框架? 我认为有三个维度来说明:这个框架是为了解决什么问题而诞生的?这个框架的核心思想是什么?这个框架适合应用到哪些场景? 说到思想,我觉得编程的灵魂就是思想,没有思想的编程和咸鱼没什么区别,这里我六年来血与泪的总结 2.不要被框架拉着走,要做框架的主人 我发现我身边好多人都深陷于框架之中,包括我自己有一段时间也深陷其中(还好现在走出来了),都关注在这个框架怎么用,哪个牛X,熟不知还没等你熟练怎么用时,又一个新的框架出来,那时你又得学,一来二去你就被框架拉着走了 我觉得当你了解上

android百度地图错误---手机显示只有一个框架,没有地图内容。

安卓新手开发百度地图,刚开始就碰到了一个问题,就是里面没有地图信息,只有一个框架而已.如图: 上网寻找说是key的问题,然后重新申请,还是不行. 最后再次看了自己的Manifest文件,发现自己的<MataData>有问题,放在了新写的Application标签里.应该放在自带的Application里面.然后,就解决了. 部分的Manifest文件: <application android:allowBackup="true" android:icon="

自定义的一个数据输入类

package xinhuiji_day07; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.text.SimpleDateFormat;import java.util.Date; public class InputData {    private BufferedReader buf = null;    public InputD