package cn.org.farseer.android.bottombutton;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.org.farseer.android.Demo;
import cn.org.farseer.android.R;
import cn.org.farseer.android.util.GlobalUtils;
/**
* Author : 简洋
* Date : 2011-9-22
*
* Note :
*/
public class BottomMenuLayout extends LinearLayout {
//实例化layout使用的类
private LayoutInflater mInflater;
//保存菜单按钮的集合,每一个集合元素代表一个按钮,包含了按钮所需要的信息:图片,文字,按键处理事件。
private List<BottomButton> bottomButtons;
//封装菜单按钮的布局。
private View bottomMenuLayout;
/**
* 该值需要根据按钮背景图片来调整。
*/
public static final int bottom_layoutHeight = 65;
public BottomMenuLayout(Context context)
{
super(context);
}
public BottomMenuLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public void processInitButton()
{
//初始化布局,将底部菜单layout加入到视图中。
initLayout(this.getContext());
//绑定每一个菜单按钮
bindingButton();
//重新计算整个布局的大小,使用整个屏幕的高度减去底部菜单的高度,
//得出并设置中间页面部分的高度,就能够将菜单固定在底部。
resizeLayout();
}
private void initLayout(Context context)
{
this.mInflater = LayoutInflater.from(context);
bottomMenuLayout = mInflater.inflate(R.layout.bottom_menu_layout, null);
addView(bottomMenuLayout);
}
private void resizeLayout()
{
View customView = getChildAt(0);
android.view.ViewGroup.LayoutParams params = customView.getLayoutParams();
int screenHeight = GlobalUtils.getInstance().getScreenHeight();
int lessHeight = screenHeight - bottom_layoutHeight;
params.height = lessHeight;
customView.setLayoutParams(params);
}
private void bindingButton() {
LinearLayout buttonGroup = (LinearLayout)findViewById(R.id.bottom_menu_button_group_id);
//初始化底部菜单按钮。
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
if(this.bottomButtons != null && this.bottomButtons.size() > 0)
{
//根据按钮数量计算按钮所占的宽度,默认要多加一个主菜单按钮一起算。
//获取屏幕宽度
int screenWidth = GlobalUtils.getInstance().getScreenWidth();
layoutParams.width = screenWidth / (bottomButtons.size() + 1);
//绑定每一个按钮
for (int i = 0; i < bottomButtons.size(); i++)
{
BottomButton oneButton = bottomButtons.get(i);
//获取底部菜单按钮布局片段。
View buttonFrameInstance = mInflater.inflate(R.layout.bottom_menu_button_frame, null);
TextView templateText = (TextView)buttonFrameInstance.findViewById(R.id.bottom_menu_template_text_id);
templateText.setText(oneButton.getText());
//templateText.setBackgroundResource(oneButton.getBackgroundResource());
ImageView templateImg = (ImageView)buttonFrameInstance.findViewById(R.id.bottom_menu_template_img_id);
templateImg.setImageResource(oneButton.getBackgroundResource());
//如果不是第一个按钮,则需要增加分割线
if(i != 0)
{
ImageView fenge = new ImageView(buttonFrameInstance.getContext());
fenge.setImageResource(R.drawable.line);
fenge.setLayoutParams(params);
buttonGroup.addView(fenge);
}
//将按钮增加到菜单栏
buttonGroup.addView(buttonFrameInstance);
//设置监听事件。
buttonFrameInstance.setOnClickListener(oneButton.getClickListener());
//设置按钮背景的宽度和背景图片
LinearLayout templateButtonLayout = (LinearLayout)buttonFrameInstance.findViewById(R.id.bottom_menu_template_button_id);
templateButtonLayout.setLayoutParams(layoutParams);
if(oneButton.isCurrent())
templateButtonLayout.setBackgroundResource(R.drawable.tab_two_highlight);
else
templateButtonLayout.setBackgroundResource(R.drawable.tab_one_normal);
}
}
//默认增加主菜单的按钮
View buttonFrameInstance = mInflater.inflate(R.layout.bottom_menu_button_frame, null);
TextView templateText = (TextView)buttonFrameInstance.findViewById(R.id.bottom_menu_template_text_id);
templateText.setText("主菜单");
ImageView templateImg = (ImageView)buttonFrameInstance.findViewById(R.id.bottom_menu_template_img_id);
templateImg.setImageResource(R.drawable.home);
//templateText.setBackgroundResource(R.drawable.home);
if(this.bottomButtons != null && this.bottomButtons.size() > 0)
{
ImageView fenge = new ImageView(buttonFrameInstance.getContext());
fenge.setImageResource(R.drawable.line);
fenge.setLayoutParams(params);
buttonGroup.addView(fenge);
}
buttonGroup.addView(buttonFrameInstance);
LinearLayout templateButtonLayout = (LinearLayout)buttonFrameInstance.findViewById(R.id.bottom_menu_template_button_id);
templateButtonLayout.setLayoutParams(layoutParams);
templateButtonLayout.setBackgroundResource(R.drawable.tab_one_normal);
buttonFrameInstance.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
v.getContext().startActivity(new Intent(v.getContext(),Demo.class));
}
});
}
public void setButtonList(List<BottomButton> bottomButtons)
{
this.bottomButtons = bottomButtons;
}
}
Android - 底部菜单架构设计及封装实现
5星 · 超过95%的资源 需积分: 12 164 浏览量
2011-10-27
15:48:35
上传
评论 1
收藏 79KB RAR 举报
o0闪电奇迹0o
- 粉丝: 17
- 资源: 6
最新资源
- 二叉树7-1-1.cpp
- android 9.0 原生模拟器 签名文件
- 技术面试最后反问面试官的话 校招面试非技术问题有哪些 非技术问题如何回答.png
- NB-IOT-BC26全网通模块Altium+ CADENCE +PADS三种格式(原理图SCH+PCB封装库)文件.zip
- 基于微信小程序开发的校园失物招领系统源码毕业设计(优质项目源码).zip
- 词向量是一种将自然语言中的单词转换为数值向量的技术,它能够捕捉词义和上下文信息
- nmap与masscan的简单使用
- MyBatis动态SQL.pdf
- 基于stm32单片机protues仿真的温湿度控制系统设计(仿真图、源代码)
- 词向量:自然语言处理的基石
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页