Android自定义控件实现底部导航
来源:互联网 发布:51wan软件 编辑:程序博客网 时间:2024/06/05 17:05
Android自定义控件实现底部导航
大部分项目中主界面都会看到底部有四个按钮切换内容,实现方法很多,我们公司3个项目都用的这种很繁琐,本文主要使用自定义控件实现以后可以直接拿来用,可以根据自己的需求扩展修改。
先上一波效果图
代码
首先在res -values 目录下新建attrs.xml文件,如下图
然后新建一个BottomMenu类继承LinearLayout
package com.example.administrator.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by Administrator on 2016/7/21 0021.
*/
public class BottomMenu extends LinearLayout {
private TextView tvmenu;private ImageView ivmenu;private int imgNormal,imgSelect,tvNormal,tvSelect;private boolean isSelect=false;private Fragment fragment;public BottomMenu(Context context) { super(context);}public BottomMenu(Context context, AttributeSet attrs) { super(context, attrs); init(context,attrs);}private void init(Context context,AttributeSet attrs) { //加载布局自定义layout_bottom_menu布局 View view = View.inflate(context,R.layout.layout_bottom_menu,this); //初始化控件 tvmenu = (TextView) view.findViewById(R.id.tv); ivmenu = (ImageView) view.findViewById(R.id.iv); //自定义属性 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BottomMenu); String text = typedArray.getString(R.styleable.BottomMenu_text); tvmenu.setText(text); //找到attrs文件里自定义的属性 imgNormal = typedArray.getResourceId(R.styleable.BottomMenu_imag_normal,0); imgSelect = typedArray.getResourceId(R.styleable.BottomMenu_imag_selected,0); tvNormal = typedArray.getColor(R.styleable.BottomMenu_text_color_normal,0); tvSelect = typedArray.getColor(R.styleable.BottomMenu_text_color_selected,0); // tvmenu.setTextColor(Color.rgb(43,43,43)); tvmenu.setTextColor(tvNormal);//设置文字颜色 ivmenu.setImageResource(imgNormal);//设置图片}public void selectMenu() { isSelect=true; ivmenu.setImageResource(imgSelect); //tvmenu.setTextColor(Color.rgb(79,167,216)); tvmenu.setTextColor(tvSelect);}public void unSelectMenu() { isSelect=false; ivmenu.setImageResource(imgNormal); // tvmenu.setTextColor(Color.rgb(43,43,43)); tvmenu.setTextColor(tvNormal);}public Fragment getmFragment() { return fragment;}public void setmFragment(Fragment mFragment) { this.fragment = mFragment;}public boolean isSelect(){ return isSelect;}
}
layout_bottom_menu xml代码
到此自定义控件就写完了
下面是使用方法
activity_main
MainActivity
package com.example.administrator.myapplication;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
public class MainActivity extends BaseActivity {
private BottomMenu mBottomMenuHome,mBottomMenuB,mBottomMenuC,mBottomMenuD;private BottomMenu lastBottomMenu;private Fragment mFragmentHome,mFragmentB,mFragmentC,mFragmentD;private long lastClickT; //点击事件private View.OnClickListener mOnClickListener = new View.OnClickListener() { @Override public void onClick(View view) { BottomMenu bm = (BottomMenu) view; if (bm.isSelect()){//当选中后不可以点击 return; } bm.selectMenu(); if (lastBottomMenu!=null){ lastBottomMenu.unSelectMenu(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.hide(lastBottomMenu.getmFragment()); transaction.show(bm.getmFragment()); transaction.commit(); } lastBottomMenu =bm; }};@Overrideprotected int setViewId() { return R.layout.activity_main;}@Overrideprotected void findViews() { //初始化控件 mBottomMenuHome = (BottomMenu) findViewById(R.id.home_fragment); mBottomMenuB = (BottomMenu) findViewById(R.id.b_fragment); mBottomMenuC = (BottomMenu) findViewById(R.id.c_frgament); mBottomMenuD = (BottomMenu) findViewById(R.id.d_fragment);}@Overrideprotected void initEvent() { mBottomMenuHome.setOnClickListener(mOnClickListener); mBottomMenuB.setOnClickListener(mOnClickListener); mBottomMenuC.setOnClickListener(mOnClickListener); mBottomMenuD.setOnClickListener(mOnClickListener);}@Overrideprotected void init() { // 初始化四个fragment mFragmentHome = new HomeFragment(); mFragmentB = new BFragment(); mFragmentC = new CFragment(); mFragmentD = new DFragment(); mBottomMenuHome.setmFragment(mFragmentHome); mBottomMenuB.setmFragment(mFragmentB); mBottomMenuC.setmFragment(mFragmentC); mBottomMenuD.setmFragment(mFragmentD); //添加四个fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.rl_conteainer,mFragmentHome); transaction.add(R.id.rl_conteainer,mFragmentB); transaction.add(R.id.rl_conteainer,mFragmentC); transaction.add(R.id.rl_conteainer,mFragmentD); //预约,商城,我的 三个fragment 隐藏 transaction.hide(mFragmentB); transaction.hide(mFragmentC); transaction.hide(mFragmentD); transaction.commit(); new Handler().post(new Runnable() { @Override public void run() { mBottomMenuHome.performClick();//模拟点击事件 让首页选中 } });}@Overrideprotected void loadData() {}
}
baseActivity
以上就是基本使用
我们还可以加入动画比如
在BottomMenu增加
public class BottomMenu extends LinearLayout {
private TextView tvmenu;private ImageView ivmenu;private int imgNormal,imgSelect,tvNormal,tvSelect;private float FACTOR=0.3F;private boolean isSelect=false;private Fragment fragment;public BottomMenu(Context context) { super(context);}public BottomMenu(Context context, AttributeSet attrs) { super(context, attrs); init(context,attrs);}private void init(Context context,AttributeSet attrs) { //加载布局 View view = View.inflate(context,R.layout.layout_bottom_menu,this); //初始化控件 tvmenu = (TextView) view.findViewById(R.id.tv); ivmenu = (ImageView) view.findViewById(R.id.iv); //自定义属性 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BottomMenu); String text = typedArray.getString(R.styleable.BottomMenu_text); tvmenu.setText(text); imgNormal = typedArray.getResourceId(R.styleable.BottomMenu_imag_normal,0); imgSelect = typedArray.getResourceId(R.styleable.BottomMenu_imag_selected,0); tvNormal = typedArray.getColor(R.styleable.BottomMenu_text_color_normal,0); tvSelect = typedArray.getColor(R.styleable.BottomMenu_text_color_selected,0); // tvmenu.setTextColor(Color.rgb(43,43,43)); tvmenu.setTextColor(tvNormal); ivmenu.setImageResource(imgNormal);}public void selectMenu() { isSelect=true; ivmenu.setImageResource(imgSelect); //tvmenu.setTextColor(Color.rgb(79,167,216)); tvmenu.setTextColor(tvSelect); tvmenu.setVisibility(INVISIBLE); ObjectAnimator animator = ObjectAnimator.ofFloat(this, "xxx", 0, 1).setDuration(300); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float progress= (float) animation.getAnimatedValue();
// Log.i(“info”,”progress”+progress);
updateView(progress);
}
});
animator.start();
}
public void unSelectMenu() { isSelect=false; ivmenu.setImageResource(imgNormal); // tvmenu.setTextColor(Color.rgb(43,43,43)); tvmenu.setTextColor(tvNormal); tvmenu.setVisibility(VISIBLE); ObjectAnimator animator = ObjectAnimator.ofFloat(this, "xxx", 1, 0).setDuration(300); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float progress= (float) animation.getAnimatedValue(); Log.i("info","progress"+progress); updateView(progress); } }); animator.start();}private void updateView(float progress) { setPivotX(getWidth()/2); setPivotY(0); setScaleX(1+FACTOR*progress); setScaleY(1+FACTOR*progress);}public Fragment getmFragment() { return fragment;}public void setmFragment(Fragment mFragment) { this.fragment = mFragment;}public boolean isSelect(){ return isSelect;}
}
- Android自定义控件实现底部导航
- Android自定义控件实现底部菜单栏
- Android 打造底部导航控件
- Android实现自定义菜单、底部导航条(一)
- 底部导航栏(自定义View+ViewPager实现) android项目详解
- Android自定义控件实现导航条IndicatorView
- 自定义底部导航栏控件NavigationButton
- Android 实现底部导航栏
- Android Fragment实现底部导航
- 实现Android底部导航栏
- Android实现底部导航栏
- android 自定义导航控件
- Android 自定义底部上拉控件的实现
- Android 自定义底部导航栏&消息显示
- Android 自定义底部导航栏&消息显示
- 【Android界面实现】带有指示器的自定义底部导航栏的实现
- Android解析自定义属性的XML实现底部导航栏TabSelectedView,实现灵活的配置扩展
- android 自定义导航控件源码
- python数据类型-元组(tuple)
- 魔镜VR,如何使UI在视野中左右静止,上下旋转
- RefreshLayout让你半小时以内完成一个列表界面
- 删除表的字段时,ID值同时也能删除
- 对于C语言中int main(int argc,char **argv)的理解
- Android自定义控件实现底部导航
- Java String.split()用法小结
- ant+proguard签名打包 .jar
- Java学习笔记之List
- GetVersionEx 获取系统版本信息
- Java poi Excel xls列号数字转字母
- 第十四周项目7—是否二叉排序树
- jQuery 动画篇
- PHP 技能树