Android自定义View——从零开始实现可展开收起的水平菜单栏

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>

效果如图

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传


绘制菜单栏按钮

我们要绘制菜单栏两边的按钮,首先是要为按钮圈地(测量位置和大小)。设置按钮区域为正方形,位于左侧或右侧(根据开发者设置而定)边长和菜单栏ViewGroup的高相等。按钮中的加号可以使用Path进行绘制(当然也可以用矢量图位图),代码如下

public class HorizontalExpandMenu extends RelativeLayout {
//省略部分代码…
private float buttonIconDegrees;//按钮icon符号竖线的旋转角度
private float buttonIconSize;//按钮icon符号的大小
private float buttonIconStrokeWidth;//按钮icon符号的粗细
private int buttonIconColor;//按钮icon颜色

private int buttonStyle;//按钮类型
private int buttonRadius;//按钮矩形区域内圆半径
private float buttonTop;//按钮矩形区域top值
private float buttonBottom;//按钮矩形区域bottom值

private Point rightButtonCenter;//右按钮中点
private float rightButtonLeft;//右按钮矩形区域left值
private float rightButtonRight;//右按钮矩形区域right值

private Point leftButtonCenter;//左按钮中点
private float leftButtonLeft;//左按钮矩形区域left值
private float leftButtonRight;//左按钮矩形区域right值

/**

  • 根按钮所在位置,默认为右边
    */
    public class ButtonStyle {
    public static final int Right = 0;
    public static final int Left = 1;
    }

private void init(){
//省略部分代码…
buttonStyle = typedArray.getInteger(R.styleable.HorizontalExpandMenu_button_style,ButtonStyle.Right);
buttonIconDegrees = 0;
buttonIconSize = typedArray.getDimension(R.styleable.HorizontalExpandMenu_button_icon_size,DpOrPxUtils.dip2px(mContext,8));
b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值