private int menuBackColor;//菜单栏背景色
private float menuStrokeSize;//菜单栏边框线的size
private int menuStrokeColor;//菜单栏边框线的颜色
private float menuCornerRadius;//菜单栏圆角半径
public HorizontalExpandMenu(Context context) {
super(context);
this.mContext = context;
init();
}
public H
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
orizontalExpandMenu(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
this.mAttrs = attrs;
init();
}
private void init(){
TypedArray typedArray = mContext.obtainStyledAttributes(mAttrs, R.styleable.HorizontalExpandMenu);
defaultWidth = DpOrPxUtils.dip2px(mContext,200);
defaultHeight = DpOrPxUtils.dip2px(mContext,40);
menuBackColor = typedArray.getColor(R.styleable.HorizontalExpandMenu_back_color,Color.WHITE);
menuStrokeSize = typedArray.getDimension(R.styleable.HorizontalExpandMenu_stroke_size,1);
menuStrokeColor = typedArray.getColor(R.styleable.HorizontalExpandMenu_stroke_color,Color.GRAY);
menuCornerRadius = typedArray.getDimension(R.styleable.HorizontalExpandMenu_corner_radius,DpOrPxUtils.dip2px(mContext,20));
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = measureSize(defaultHeight, heightMeasureSpec);
int width = measureSize(defaultWidth, widthMeasureSpec);
viewHeight = height;
viewWidth = width;
setMeasuredDimension(viewWidth,viewHeight);
//布局代码中如果没有设置background属性则在此处添加一个背景
if(getBackground()==null){
setMenuBackground();
}
}
private int measureSize(int defaultSize, int measureSpec) {
int result = defaultSize;
int specMode = View.MeasureSpec.getMode(measureSpec);
int specSize = View.MeasureSpec.getSize(measureSpec);
if (specMode == View.MeasureSpec.EXACTLY) {
result = specSize;
} else if (specMode == View.MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
return result;
}
/**
- 设置菜单背景,如果要显示阴影,需在onLayout之前调用
*/
private void setMenuBackground(){
GradientDrawable gd = new GradientDrawable();
gd.setColor(menuBackColor);
gd.setStroke((int)menuStrokeSize, menuStrokeColor);
gd.setCornerRadius(menuCornerRadius);
setBackground(gd);
}
}
attrs属性
<?xml version="1.0" encoding="utf-8"?>在布局文件中使用
<com.anlia.expandmenu.widget.HorizontalExpandMenu
android:id="@+id/expandMenu1"
android:layout_width=“match_parent”
android:layout_height=“40dp”
android:layout_alignParentBottom=“true”
android:layout_marginBottom=“20dp”
android:layout_marginLeft=“15dp”
android:layout_marginRight=“15dp”>
</com.anlia.expandmenu.widget.HorizontalExpandMenu>
效果如图
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eaoLwwHw-163713852711