android系统中ToolBar的高度是默认的,貌似google不希望我们能修改这个控件的高度,我们可以在xml中修改ToolBar的高度,可是这样的结果我们可以看出来,天啊!ToolBar布局里边的控件全部都乱套了,布局混乱了。这显然是我们不能接受的,其实我们在开发过程中是不用修改这个控件的,但是我们知道一般公司的设计师只会出一套设计图,而且还是IOS的,IOS默认的ToolBar高度是44dp,而android<dimen name="abc_action_bar_default_height_material">56dp</dimen>。可以看出是56dp。给人感觉是要高的多。所以我们强迫症出来了,要修改这个控件,那么今天就给大家一个自定义的完美解决高度的ToolBar控件。直接上代码:
package com.hdceping.www.feirunning.view.custom;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v4.media.RatingCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.hdceping.www.feirunning.R;
import java.lang.reflect.Field;
/**
* Created by 晁东洋 on 2016/10/28.
*/
public class MyToolbar extends Toolbar {
private TextView mTitleTextView;
private CharSequence mTitleText;
private int mTitleTextColor;
private int mTitleTextAppearance;
public MyToolbar(Context context) {
super(context);
resolveAttribute(context, null, R.attr.toolbarStyle);
}
public MyToolbar(Context context,@Nullable AttributeSet attrs) {
super(context,attrs);
resolveAttribute(context,attrs,R.attr.toolbarStyle);
}
public MyToolbar(Context context,@Nullable AttributeSet attrs, int defStyleAttr) {
super(context,attrs,defStyleAttr);
resolveAttribute(context,attrs,defStyleAttr);
}
private void resolveAttribute(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
// Need to use getContext() here so that we use the themed context
context = getContext();
final TintTypedArray a = TintTypedArray.obtainStyledAttributes(context,attrs,
R.styleable.Toolbar,defStyleAttr,0);
final int titleTextAppearance = a.getResourceId(R.styleable.Toolbar_titleTextAppearance,0);
if(titleTextAppearance !=0) {
setTitleTextAppearance(context,titleTextAppearance);
}
if(mTitleTextColor!=0) {
setTitleTextColor(mTitleTextColor);
}
a.recycle();
post(new Runnable() {
@Override
public void run() {
if(getLayoutParams()instanceof LayoutParams) {
((LayoutParams) getLayoutParams()).gravity= Gravity.CENTER;
}
}
});
}
@Override
public CharSequence getTitle() {
return mTitleText;
}
@Override
public void setTitle(CharSequence title) {
if(!TextUtils.isEmpty(title)) {
if(mTitleTextView==null) {
final Context context = getContext();
mTitleTextView=new TextView(context);
mTitleTextView.setSingleLine();
mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
if(mTitleTextAppearance!=0) {
mTitleTextView.setTextAppearance(context,mTitleTextAppearance);
}
if(mTitleTextColor!=0) {
mTitleTextView.setTextColor(mTitleTextColor);
}
}
if(mTitleTextView.getParent() !=this) {
addCenterView(mTitleTextView);
}
}else if(mTitleTextView!=null&&mTitleTextView.getParent() ==this) {// 当title为空时,remove
removeView(mTitleTextView);
}
if(mTitleTextView!=null) {
mTitleTextView.setText(title);
}
mTitleText= title;
}
private void addCenterView(View v) {
final ViewGroup.LayoutParams vlp = v.getLayoutParams();
final LayoutParams lp;
if(vlp ==null) {
lp = generateDefaultLayoutParams();
}else if(!checkLayoutParams(vlp)) {
lp = generateLayoutParams(vlp);
}else{
lp = (LayoutParams) vlp;
}
addView(v,lp);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
LayoutParams lp =new LayoutParams(getContext(),attrs);
lp.gravity= Gravity.CENTER;
return lp;
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
LayoutParams lp;
if(p instanceof LayoutParams) {
lp =new LayoutParams((LayoutParams) p);
}else if(p instanceof ActionBar.LayoutParams) {
lp =new LayoutParams((ActionBar.LayoutParams) p);
}else if(p instanceof MarginLayoutParams) {
lp =new LayoutParams((MarginLayoutParams) p);
}else{
lp =new LayoutParams(p);
}
lp.gravity= Gravity.CENTER;
return lp;
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
LayoutParams lp =new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.gravity= Gravity.CENTER;
return lp;
}
@Override
public void setTitleTextAppearance(Context context, @RatingCompat.Style int resId) {
mTitleTextAppearance= resId;
if(mTitleTextView!=null) {
mTitleTextView.setTextAppearance(context,resId);
}
}
@Override
public void setTitleTextColor(@ColorInt int color) {
mTitleTextColor= color;
if(mTitleTextView!=null) {
mTitleTextView.setTextColor(color);
}
}
@Override
public void setNavigationIcon(@Nullable Drawable icon) {
super.setNavigationIcon(icon);
setGravityCenter();
}
public void setGravityCenter() {
post(new Runnable() {
@Override
public void run() {
setCenter("mNavButtonView");
setCenter("mMenuView");
}
});
}
private void setCenter(String fieldName) {
try{
Field field = getClass().getSuperclass().getDeclaredField(fieldName);//反射得到父类Field
field.setAccessible(true);
Object obj = field.get(this);//拿到对应的Object
if(obj ==null)return;
if(obj instanceof View) {
View view = (View) obj;
ViewGroup.LayoutParams lp = view.getLayoutParams();//拿到LayoutParams
if(lp instanceof ActionBar.LayoutParams) {
ActionBar.LayoutParams params = (ActionBar.LayoutParams) lp;
params.gravity= Gravity.CENTER;//设置居中
view.setLayoutParams(lp);
}
}
}catch(NoSuchFieldException e) {
e.printStackTrace();
}catch(IllegalAccessException e) {
e.printStackTrace();
}
}
}
由于其中的细节问题还未搞明白,大家可以一起探讨一下。原文地址:http://www.jianshu.com/p/621225a55561
博客已经移步:chaodongyang.com
喜欢就关注我的微信公众号吧,更多干货分享