• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > 关于TabLayout的使用 ,自定义了一个框架。。。 以后写底部菜单就可以直接作为依赖库 ,不用麻烦了,tablayout框架

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

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了关于TabLayout的使用 ,自定义了一个框架。。。 以后写底部菜单就可以直接作为依赖库 ,不用麻烦了,tablayout框架等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

关于TabLayout的使用 ,自定义了一个框架。。。 以后写底部菜单就可以直接作为依赖库 ,不用麻烦了,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
* 这

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • 2017-05-26在Android studio中进行单元测试和ui测试的分析
  • 2017-05-26Android自定义控件(二),android自定义控件
  • 2017-05-26ListView的CheckBox实现全部选中/不选中,listviewcheckbox
  • 2017-05-26Android开发:消息机制简述
  • 2017-05-26如何挂载ubi文件系统
  • 2017-05-26DB2_Tips_Tricks_04 远程数据库之间复制数据
  • 2017-05-26Shell 下求两个文件交集、并集和差集的办法
  • 2017-05-26ELK部署参考文档
  • 2017-05-26保持listview当前位置,listview当前位置
  • 2017-05-26安卓6.0(棉花糖)新特性汇总,安卓6.0

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 使用Android studio创建的AIDL编译时找不到自定义类的解决办法,androidaidl
    • Android数据存储之SQLite
    • Android IPC 之 AIDL(一)
    • android获取屏幕宽高与获取控件宽高,android获取控件
    • 8.2.2 Bitmap引起的OOM问题
    • android源码解析之(二)--)异步任务AsyncTask
    • Android中使用开源框架PagerSlidingTabStrip实现导航标题,android开源框架
    • 安卓开源项目周报1227,安卓开源项目1227
    • Handler消息传递机制(一)
    • 缓存之 ACache

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有