样式:
1、
2、
3、
4、
导航栏中间可设置icon(如样式2),也可以设置文字(如样式1),右侧有一个按钮可设置文字和icon,左侧有两个按钮目前是点击事件是合二为一的,也可以自行设置。在样式4中采用了沉浸式,系统状态栏设置成透明,再添加底部颜色(仅适用Android4.4及其以上版本)。对于沉浸式中,系统状态栏的字体颜色是否可以更改,本文给出了仅支持小米MIUI6以后版本的修改方案,不过只能改成黑色或者白色。调用方法如下:
/**改变标题栏字体颜色,仅适用于小米miui6,false白色,true黑色
*
*/
public void setStatusBarDarkMode(boolean darkmode, Activity activity)
源码:
1、BaseUtils//基础工具类
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
public class BaseUtils {
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
if (context!=null) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}else{
return 0;
}
}
}
2、AnimationUtil //动画工具类
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import base.BaseConstant;
public class AnimationUtil {
/**
* 是否开启动画
*
*/
public static boolean isAnimation = true;
/**缩放
* @param view
* @param from 缩放开始比例0-1.0
* @param to 缩放结束比例0-1.0
* @param w
* @param h
*/
public static void setViewScale(View view,float from,float to,int w,int h) {
if (isAnimation) {
ScaleAnimation animation = new ScaleAnimation(from, to, from, to,
w/2, h/2);
animation.setDuration(200);
animation.setFillAfter(true);
view.clearAnimation();
view.setAnimation(animation);
animation = null;
System.gc();
}
}
}
3、系统状态栏(这里我导成jar了),详细代码可以参考
http://blog.csdn.net/jdsjlzx/article/details/41643587
4、TopTitle(详细请看代码注释,附有调用例子)
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import cn.focusspot.zhongchengbus.R;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import tools.AnimationUtil;
import tools.BaseUtils;
import android.view.View.OnTouchListener;
/**
* @author Administrator
* 调用例子:
* toptitle=(TopTitle) findViewById(R.id.toptitle);
toptitle.setTitleTextSize(20);//使用text过长会自动省略功能时,setTitleTextSize需在setTitle前
toptitle.setTitle("众乘巴士");
toptitle.setTitleTextColor(Color.parseColor("#000000"));
toptitle.setLeftImage(R.drawable.pic_10);
toptitle.setRightButtonVisibility(false);
toptitle.setHight(getWindowManager().getDefaultDisplay()
.getHeight() / 12);
toptitle.setBackground(Color.parseColor("#ffffff"));
toptitle.setOnTitleClickListener(new onTitleClickListener() {
@Override
public void rightClick(String titleString) {
// TODO Auto-generated method stub
}
@Override
public void leftClick(String titleString) {
// TODO Auto-generated method stub
finish();
}
});
*/
public class TopTitle extends RelativeLayout implements OnClickListener , OnTouchListener{
private Context context;
/**左侧button,多为返回icon
*
*/
private Button leftButton;
private final int leftButtonId=1;
/**左侧button,多为"返回"字样
*
*/
private Button leftButton2;
private final int leftButton2Id=2;
/**左侧button,多为"返回"字样,是否显示
*
*/
private boolean isBack = true;
/**右侧button,多为菜单icon
*
*/
private Button rightButton;
private final int rightButtonId=3;
/**leftParams 左侧布局
* rightParams 右侧布局
* titleParams 中间布局
*/
private LayoutParams leftParams,rightParams,titleParams;
/**leftLayout 左侧布局
* rightLayout 右侧布局
* Layout 中间布局
*/
private LinearLayout Layout,leftLayout,rightLayout;
private final int LayoutId=4;
private final int leftLayoutId=5;
private final int rightLayoutId=6;
/**右侧布局是否显示
*
*/
private boolean rightView=true;
/**左侧侧布局是否显示
*
*/
private boolean leftView=true;
/**超长文本自动添加省略号
*
*/
private MyTextView titleTital;
/**背景颜色
*
*/
private int backColor=Color.rgb(74,148,255);
/**中间title字体大小,单位sp
*
*/
private float titleTextSize=20;
/**title字体颜色
*
*/
private int titleTextColor=Color.BLACK;
/**title内容
*
*/
private String titleString="title";
/**顶部导航栏高度
*
*/
private int hight=20;
/**系统状态栏高度,应用于沉浸式
*
*/
private int systembarHight=0;
/**是否使用沉浸式
*
*/
private boolean systemBar=false;
/**是否小米miui6系统
*
*/
private boolean isMiui6=false;
/**true title在中间位置
* false title在左边位置
*/
private boolean center=true;
/**title 左边的距离
* 应用于center=false
*/
private int leftDistance;
/**设置背景颜色
* @param color
*/
public void setBackground(int color){
this.backColor=color;
setBackgroundColor(color);
}
/**设置中间icon
* @param res
*/
public void setTitleImage(int res){
titleTital.getLayoutParams().width=hight*5/4;//调整图片比例,可以不要,根据具体情况
titleTital.setBackgroundResource(res);
}
public interface onTitleClickListener{
/**左侧按钮监听
* @param titleString
*/
public void leftClick(String titleString);
/**右侧按钮监听
* @param titleString
*/
public void rightClick(String titleString);
}
public onTitleClickListener listener=null;
public void setOnTitleClickListener(onTitleClickListener listener){
this.listener=listener;
}
public int getHight() {
return hight;
}
/**设置高度
*
*/
public void setHight(int hight) {
if (systemBar) {
systembarHight=getStatusHeight(context);
}
this.hight = hight+systembarHight;
getLayoutParams().height=this.hight;
//设置边距,dip2px()根据手机的分辨率从 dp 的单位 转成为 px(像素)
rightLayout.setPadding( BaseUtils.dip2px(context, 10), systembarHight,BaseUtils.dip2px(context,10), 0);
leftLayout.setPadding(BaseUtils.dip2px(context, 10), systembarHight, BaseUtils.dip2px(context, 10), 0);
Layout.setPadding(0, systembarHight, 0, 0);
}
/**获取系统状态栏高度
*
* @param activity
* @return > 0 success; <= 0 fail
*/
public static int getStatusHeight(Context activity){
int statusHeight = 0;
Rect localRect = new Rect();
((Activity) activity).getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
statusHeight = localRect.top;
if (0 == statusHeight){
Class<?> localClass;
try {
localClass = Class.forName("com.android.internal.R$dimen");
Object localObject = localClass.newInstance();
int i5 = Integer.parseInt(localClass.getField("status_bar_height").get(localObject).toString());
statusHeight = activity.getResources().getDimensionPixelSize(i5);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
return statusHeight;
}
/**左边按钮是否可用
* @param frag
*/
public void setLeftButtonVisibility(boolean frag){
if(frag)leftButton.setVisibility(VISIBLE);
else leftButton.setVisibility(GONE);
leftView=frag;
}
/**右边按钮是否可用
* @param frag
*/
public void setRightButtonVisibility(boolean frag){
if(frag)rightButton.setVisibility(VISIBLE);
else rightButton.setVisibility(GONE);
rightView=frag;
}
/**设置title字体颜色
* @param color
*/
public void setTitleTextColor(int color){
this.titleTextColor=color;
titleTital.setTextColor(color);
}
/**设置title字体大小
* @param size
*/
public void setTitleTextSize(float size){
titleTextSize=size;
titleTital.setTextSize(size);
}
/**设置title
* @param title
*/
public void setTitle(String title){
titleTital.setTextMy(title,titleTextSize,BaseUtils.dip2px(context, 200));
}
/**获取title
* @return
*/
public String getTitle(){
return titleTital.getText().toString();
}
/**设置左侧button2,"返回"字样是否显示
* @param is
*/
public void setLeftBack(boolean is){
isBack=is;
if (is) {
leftButton2.setVisibility(View.VISIBLE);
}else{
leftButton2.setVisibility(View.GONE);
}
}
/**设置左侧icon
* @param reg
*/
public void setLeftImage(int reg){
leftButton.setText("");
leftButton.getLayoutParams().height=BaseUtils.dip2px(context, 20);
leftButton.getLayoutParams().width=BaseUtils.dip2px(context, 20);
leftButton.setBackgroundResource(reg);
}
/**设置右侧icon
* @param reg
*/
public void setRightImage(int reg){
rightButton.setText("");
rightButton.getLayoutParams().height=BaseUtils.dip2px(context, 20);
rightButton.getLayoutParams().width=BaseUtils.dip2px(context, 20);
rightButton.setBackgroundResource(reg);
}
public String getLeftText(){
return leftButton.getText().toString();
}
public String getRightText(){
return rightButton.getText().toString();
}
/**设置右侧text
* @param text
*/
@SuppressLint("NewApi")
public void setRightText(String text){
rightButton.setBackground(null);
rightButton.getLayoutParams().height=LinearLayout.LayoutParams.WRAP_CONTENT;
rightButton.getLayoutParams().width=LinearLayout.LayoutParams.WRAP_CONTENT;
rightButton.setText(text);
rightButton.setTextSize(16);
rightButton.setTextColor(Color.BLACK);
}
/**设置左侧text
* @param text
*/
@SuppressLint("NewApi")
public void setLeftText(String text){
leftButton.setBackground(null);
leftButton.getLayoutParams().height=LinearLayout.LayoutParams.WRAP_CONTENT;
leftButton.getLayoutParams().width=LinearLayout.LayoutParams.WRAP_CONTENT;
leftButton.setText(text);
leftButton.setTextSize(16);
leftButton.setTextColor(Color.BLACK);
}
public TopTitle(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context=context;
init();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressLint("NewApi")
public TopTitle(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context=context;
init();
}
/**改变标题栏字体颜色,仅适用于小米miui6,false白色,true黑色
*
*/
public void setStatusBarDarkMode(boolean darkmode, Activity activity) {
Class<? extends Window> clazz = activity.getWindow().getClass();
try {
int darkModeFlag = 0;
Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
darkModeFlag = field.getInt(layoutParams);
Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
} catch (Exception e) {
e.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressLint("NewApi")
private void init(){
/**
* "手机型号: " + android.os.Build.MODEL + ",\nSDK版本:"
+ android.os.Build.VERSION.SDK + ",\n系统版本:"
+ android.os.Build.VERSION.RELEASE
*/
// if (android.os.Build.VERSION.SDK != null) {
// int sdk=Integer.parseInt(android.os.Build.VERSION.SDK);
// if(sdk>=19){
// systemBar=true;
// }else{
// systemBar=false;
// }
// }
if(systemBar){
SystemBarTintManager tintManager = new SystemBarTintManager((Activity) context);
tintManager.setStatusBarTintEnabled(false);
if (isMiui6) {
setStatusBarDarkMode(true, (Activity) context);
}
}
setBackgroundColor(backColor);
leftLayout = new LinearLayout(context);
leftButton=new Button(context);
leftButton.setLayoutParams(new LayoutParams(BaseUtils.dip2px(context, 20), BaseUtils.dip2px(context, 20)));
leftButton.setBackgroundResource(R.drawable.ic_64);
leftButton.setId(leftButtonId);
leftLayout.setId(leftLayoutId);
leftLayout.addView(leftButton);
leftButton2=new Button(context);
leftButton2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, BaseUtils.dip2px(context, 20)));
leftButton2.setBackground(null);
leftButton2.setText("返回");
leftButton2.setGravity(Gravity.CENTER_VERTICAL);
leftButton2.setTextSize(16);
leftButton2.setTextColor(Color.parseColor("#ff4000"));
leftButton2.setId(leftButton2Id);
leftButton2.setPadding(BaseUtils.dip2px(context, 5), systembarHight,BaseUtils.dip2px(context, 5), 0);
if (isBack) {
leftButton2.setVisibility(View.VISIBLE);
}else{
leftButton2.setVisibility(View.GONE);
}
leftLayout.setGravity(Gravity.CENTER_VERTICAL);
leftLayout.addView(leftButton2);
rightLayout = new LinearLayout(context);
rightButton=new Button(context);
rightButton.setBackgroundResource(R.drawable.ic_64);
// rightImageButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
rightButton.setLayoutParams(new LayoutParams(BaseUtils.dip2px(context, 20), BaseUtils.dip2px(context, 20)));
rightLayout.setPadding( BaseUtils.dip2px(context,10), systembarHight,BaseUtils.dip2px(context,10), 0);
rightLayout.setGravity(Gravity.CENTER);
rightButton.setId(rightButtonId);
rightLayout.setId(rightLayoutId);
rightLayout.addView(rightButton);
Layout = new LinearLayout(context);
titleTital=new MyTextView(context);
titleTital.setTextColor(titleTextColor);
titleTital.setTextSize(titleTextSize);
titleTital.setTextMy(titleString,titleTextSize,BaseUtils.dip2px(context, 200));
titleTital.setGravity(Gravity.CENTER);
titleTital.setSingleLine();
Layout.setGravity(Gravity.CENTER);
Layout.setPadding(leftDistance, 0, 0, 0);
Layout.setId(LayoutId);
Layout.addView(titleTital);
leftParams =new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);//����
addView(leftLayout, leftParams);
titleParams =new LayoutParams(BaseUtils.dip2px(context, 200),LayoutParams.MATCH_PARENT);
if(center){
titleTital.setGravity(Gravity.CENTER_HORIZONTAL);
titleTital.setPadding(0, systembarHight, 0, 0);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
}else{
titleTital.setPadding(leftDistance, systembarHight, 0, 0);
titleParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
}
addView(Layout, titleParams);
rightParams =new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
addView(rightLayout, rightParams);
//点击事件
leftButton.setOnClickListener(this);
leftButton2.setOnClickListener(this);
leftLayout.setOnClickListener(this);
rightButton.setOnClickListener(this);
rightLayout.setOnClickListener(this);
//触摸效果动画
leftButton.setOnTouchListener(this);
leftButton2.setOnTouchListener(this);
rightButton.setOnTouchListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case leftButtonId:
case leftButton2Id:
case leftLayoutId:
if(listener!=null)listener.leftClick(titleTital.getText().toString());
break;
case rightButtonId:
case rightLayoutId:
if(listener!=null&&rightView)listener.rightClick(titleTital.getText().toString());
if(!rightView){
// Intent intent =new Intent(context,EnterActivity.class);
// ((Activity)context).startActivity(intent);
// ((Activity)context).finish();
}
break;
default:
break;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case leftButtonId:
case leftButton2Id:
case leftLayoutId:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
AnimationUtil.setViewScale(leftButton,1f,0.7f,BaseUtils.dip2px(context, 20),BaseUtils.dip2px(context, 20) );
AnimationUtil.setViewScale(leftButton2,1f,0.7f,BaseUtils.dip2px(context, 20),BaseUtils.dip2px(context, 20) );
break;
case MotionEvent.ACTION_UP:
AnimationUtil.setViewScale(leftButton,0.7f,1f,BaseUtils.dip2px(context, 20),BaseUtils.dip2px(context, 20) );
AnimationUtil.setViewScale(leftButton2,0.7f,1f,BaseUtils.dip2px(context, 20),BaseUtils.dip2px(context, 20) );
break;
}
break;
case rightButtonId:
case rightLayoutId:
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
AnimationUtil.setViewScale(rightButton,1f,0.7f,BaseUtils.dip2px(context, 20),BaseUtils.dip2px(context, 20) );
AnimationUtil.setViewScale(rightLayout,1f,0.7f,BaseUtils.dip2px(context, 20),BaseUtils.dip2px(context, 20) );
break;
case MotionEvent.ACTION_UP:
AnimationUtil.setViewScale(rightButton,0.7f,1f,BaseUtils.dip2px(context, 20),BaseUtils.dip2px(context, 20) );
AnimationUtil.setViewScale(rightLayout,0.7f,1f,BaseUtils.dip2px(context, 20),BaseUtils.dip2px(context, 20) );
break;
}
break;
}
return false;
}
}