Android自定义TabBar
转载请说明出处:http://www.sunhome.org.cn
我发现现在的移动开发界面都被iOS主导了,UI动不动设计出来的东西都是ios的风格,对于一个做Android的程序员来说甚是苦恼啊,为了适应这种环境和氛围,今天我们来自定义一个TabBar,这个是移动开发很常用的一个组件。
<?xml version="1.0" encoding="utf-8"?>
<resources> <!-- TabBarItem -->
<declare-styleable name="TabBarItem">
<attr name="checked_item" format="boolean"></attr>
<attr name="nomal_icon" format="reference"></attr>
<attr name="check_icon" format="reference"></attr>
<attr name="text" format="string"></attr>
<attr name="text_size" format="dimension"></attr>
<attr name="nomal_color" format="color"></attr>
<attr name="check_color" format="color"></attr>
</declare-styleable> </resources>
定义界面xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:hyman="http://schemas.android.com/apk/res/com.xxx.xx"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_base_nomal_background_color"
android:orientation="vertical" > <FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"/> <View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/color_app_base_7"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dip"
android:background="@color/main_bottom_tab_bg_color"
android:orientation="horizontal">
<com.xxx.xx.view.TabBarItem
android:id="@+id/id_indicator_one"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
hyman:checked_item="true"
hyman:nomal_icon="@drawable/tab_bar_home_normal"
hyman:check_icon="@drawable/tab_bar_home_current"
hyman:text="@string/activity_main_tab1_title"
hyman:text_size="@dimen/app_nomal_text_size"
hyman:nomal_color="@color/color_app_base_2"
hyman:check_color="@color/color_app_base_1"/> <com.xxx.xx.view.TabBarItem
android:id="@+id/id_indicator_two"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
hyman:checked_item="false"
hyman:nomal_icon="@drawable/tab_bar_market_normal"
hyman:check_icon="@drawable/tab_bar_market_current"
hyman:text="@string/activity_main_tab2_title"
hyman:text_size="@dimen/app_nomal_text_size"
hyman:nomal_color="@color/color_app_base_2"
hyman:check_color="@color/color_app_base_1"/> <com.xxx.xx.view.TabBarItem
android:id="@+id/id_indicator_three"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
hyman:checked_item="false"
hyman:nomal_icon="@drawable/tab_bar_market_normal"
hyman:check_icon="@drawable/tab_bar_market_current"
hyman:text="@string/activity_main_tab3_title"
hyman:text_size="@dimen/app_nomal_text_size"
hyman:nomal_color="@color/color_app_base_2"
hyman:check_color="@color/color_app_base_1"/> <com.xxx.xx.view.TabBarItem
android:id="@+id/id_indicator_four"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
hyman:checked_item="false"
hyman:nomal_icon="@drawable/tab_bar_basket_normal"
hyman:check_icon="@drawable/tab_bar_basket_current"
hyman:text="@string/activity_main_tab4_title"
hyman:text_size="@dimen/app_nomal_text_size"
hyman:nomal_color="@color/color_app_base_2"
hyman:check_color="@color/color_app_base_1"/> <com.xxx.xx.view.TabBarItem
android:id="@+id/id_indicator_five"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
hyman:checked_item="false"
hyman:nomal_icon="@drawable/tab_bar_personal_normal"
hyman:check_icon="@drawable/tab_bar_personal_current"
hyman:text="@string/activity_main_tab5_title"
hyman:text_size="@dimen/app_nomal_text_size"
hyman:nomal_color="@color/color_app_base_2"
hyman:check_color="@color/color_app_base_1"/>
</LinearLayout>
</LinearLayout>
自定义TabBarItem
package com.guozha.buy.view; import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView; import com.guozha.buy.R;
import com.guozha.buy.util.DimenUtil;
import com.guozha.buy.util.LogUtil; /**
* TabBar的按钮控件
* @author PeggyTong
*
*/
public class TabBarItem extends LinearLayout{ private static final int ICON_HEIGHT = 32; private boolean isChoosed;
private String mTextContent;
private int mTextSize;
private int mTextNomalColor;
private int mTextCheckColor;
private Bitmap mNomalIcon;
private Bitmap mCheckIcon; private ImageView mBtnImage;
private TextView mBtnText; public TabBarItem(Context context) {
this(context, null);
} public TabBarItem(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public TabBarItem(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initTabBarItem(context, attrs);
} /**
* 初始化
* @param context
* @param attrs
*/
private void initTabBarItem(Context context, AttributeSet attrs) {
initTabItem(context, attrs); //获取尺寸
addChilds(context); //添加子view
changeCheckedStatus(); //根据状态显示当前view
} /**
* 添加子控件
* @param context
*/
private void addChilds(Context context) {
setOrientation(LinearLayout.VERTICAL);
setGravity(Gravity.CENTER); mBtnImage = new ImageView(context);
mBtnImage.setScaleType(ScaleType.CENTER_INSIDE);
mBtnImage.setLayoutParams(
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, DimenUtil.dp2px(context, ICON_HEIGHT))); mBtnText = new TextView(context);
LogUtil.e("mTextSize = " + mTextSize);
mBtnText.setText(mTextContent);
mBtnText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
mBtnText.setLayoutParams(
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(mBtnImage);
addView(mBtnText);
} /**
* 改变当前显示状态
*/
private void changeCheckedStatus() {
if(isChoosed){
mBtnImage.setImageBitmap(mCheckIcon);
mBtnText.setTextColor(mTextCheckColor);
}else{
mBtnImage.setImageBitmap(mNomalIcon);
mBtnText.setTextColor(mTextNomalColor);
}
} /**
* 设置为选中状态
*/
public void setCheckedItem(){
isChoosed = true;
changeCheckedStatus();
} /**
* 设置为未选中状态
*/
public void setDisCheckedItem(){
isChoosed = false;
changeCheckedStatus();
} /**
* 初始配置的数据
* @param context
* @param attrs
*/
private void initTabItem(Context context, AttributeSet attrs){
TypedArray typeArr = context.obtainStyledAttributes(attrs, R.styleable.TabBarItem);
int count = typeArr.getIndexCount();
for(int i = 0; i < count; i++){
int attr = typeArr.getIndex(i);
switch(attr){
case R.styleable.TabBarItem_checked_item:
isChoosed = typeArr.getBoolean(attr, false);
break;
case R.styleable.TabBarItem_text:
mTextContent = typeArr.getString(attr);
break;
case R.styleable.TabBarItem_text_size:
mTextSize = (int) typeArr.getDimension(attr, TypedValue.applyDimension
(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));
break;
case R.styleable.TabBarItem_nomal_color:
mTextNomalColor = typeArr.getColor(attr, getResources().getColor(R.color.color_app_base_2));
break;
case R.styleable.TabBarItem_check_color:
mTextCheckColor = typeArr.getColor(attr, getResources().getColor(R.color.color_app_base_1));
break;
case R.styleable.TabBarItem_nomal_icon:
mNomalIcon = ((BitmapDrawable)typeArr.getDrawable(attr)).getBitmap();
break;
case R.styleable.TabBarItem_check_icon:
mCheckIcon = ((BitmapDrawable)typeArr.getDrawable(attr)).getBitmap();
break;
}
}
typeArr.recycle();
}
}
如果是多个Tab切换,可以使用Fragment
添加初始界面的Fragment
//添加一个fragment
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, mFragments.get(0)).commit();
替换Fragment
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, mFragments.get(mCurrentItem))
.addToBackStack(null).commit();
Android自定义TabBar的更多相关文章
- 自定义tabBar
★★★★自定义tabBar★★★★★★★ Demo下载地址:https://github.com/marlonxlj/tabBarCustom.git 前言: 有的时候需求要对tabBar进行自定义的 ...
- android 自定义动画
android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...
- IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)
********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...
- Android自定义View 画弧形,文字,并增加动画效果
一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类 B ...
- iOS 隐藏自定义tabbar
iOS 隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...
- Android自定义View4——统计图View
1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...
- (转)[原] Android 自定义View 密码框 例子
遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...
- iOS开发之功能模块--关于自定义TabBar条
只上项目中用到的代码: 1.实现重写TabBar的TabBarItem,然后在中间额外加一个按钮. #import <UIKit/UIKit.h> @interface BikeTabBa ...
- Android 自定义View合集
自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...
随机推荐
- 1.Thinkphp入门--框架介绍
1.Thinkphp款家最早诞生于2006年初,最初名叫fsc,2007年元旦改名thinkphp,由国人自主开发的框架,有中文的文档和社区 2.Tinkphp下载: 官网:http://www.th ...
- JavaScript实现乘法表
JavaScript实现乘法表 <script type="text/javascript"> function c(n,m) { ...
- HTML 捕获window.close() 并做窗口关闭前的处理工作
转自:http://www.xinotes.net/notes/note/261/ <html> <head> <script language="JavaSc ...
- Nusoap复杂对象的的webService制作
推荐网址:http://www.scottnichol.com/nusoapprogwsdl.htm摘抄部分如下:服务器端程序 <?php // Pull in the NuSOAP code ...
- CF739E Gosha is hunting(费用流,期望)
根据期望的线性性答案就是捕捉每一只精灵的概率之和. 捕捉一只精灵的方案如下: 1.使用一个\(A\)精灵球,贡献为\(A[i]\) 2.使用一个\(B\)精灵球,贡献为\(B[i]\) 3.使用一个\ ...
- [COCI2007]PRAVOKUTNI
题目大意:在一个平面上,有\(N\)个点,求这些点构成的直角三角形个数.解题思路:枚举直角顶点,对于每个点,将这个点当做原点,对其他点按极角排序,然后双指针扫一遍,判断弧度差即可. C++ Code: ...
- 代理上网环境配置docker私有库
最后更新时间:2018年12月27日 Docker使用代理上网去 pull 各类 images,需要做如下配置: 创建目录: /etc/systemd/system/docker.service.d ...
- php 文件加载方式
两种加载文件的方式 include require 使用场景: 动态加载文件的时候,使用include,否则使用require. 示例: # 引入php文件--include方式 inlcude(&q ...
- 【【henuacm2016级暑期训练】动态规划专题 D】Writing Code
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二维费用背包. f[i][j][k] 前i个人,写了j行,bug不超过k的方案数. 可以把每个人看成是一个物品. 它可以无限拿.然后 ...
- HDOJ 5294 Tricks Device 最短路(记录路径)+最小割
最短路记录路径,同一时候求出最短的路径上最少要有多少条边, 然后用在最短路上的边又一次构图后求最小割. Tricks Device Time Limit: 2000/1000 MS (Java/Oth ...