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的应用中,经常会见到底部菜单.而在企业级的Android应用中,也存在同样的需求,但与微信这些大众软件的区别在于企业级的Android应用由于UI页面很多,每个页面都需要有底部菜单,而且每个页面的底部菜单按钮还可能完全不一样,所以,为了使每个页面保持一致性并为UI页面制作时提供便利,针对底部菜单进行专门的设计封装,就显得特别重要。
资源推荐
资源详情
资源评论
收起资源包目录
BottomButtonDemo.zip (39个子文件)
BottomButtonDemo
bin
classes.dex 9KB
BottomButtonDemo.apk 33KB
cn
org
farseer
android
Demo.class 2KB
bottombutton
BottomMenuLayout.class 5KB
BottomMenuLayout$1.class 1KB
BottomButton.class 1KB
R$layout.class 496B
base
BaseBottomMenuActivity.class 2KB
R$id.class 574B
R$drawable.class 576B
R$attr.class 352B
R.class 559B
R$string.class 439B
util
GlobalUtils.class 1KB
resources.ap_ 26KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable
image.png 516B
line.jpg 11KB
tab_two_highlight.png 1017B
tab_one_normal.png 1KB
home.png 493B
values
strings.xml 179B
drawable-mdpi
icon.png 3KB
layout
main.xml 367B
bottom_menu_button_frame.xml 805B
bottom_menu_layout.xml 454B
assets
default.properties 362B
gen
cn
org
farseer
android
R.java 1KB
proguard.cfg 1KB
.settings
org.eclipse.core.resources.prefs 88B
src
cn
org
farseer
android
bottombutton
BottomMenuLayout.java 6KB
BottomButton.java 1KB
base
BaseBottomMenuActivity.java 4KB
Demo.java 1KB
util
GlobalUtils.java 878B
.project 852B
.classpath 280B
AndroidManifest.xml 638B
共 39 条
- 1
cding1818
- 粉丝: 964
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页