Android 仿淘宝选中商品不同尺寸的按钮组(二)

本文针对Android仿淘宝商品尺寸选择按钮组的问题进行改进,包括支持TextView模式、添加 GoodsViewGroupItem 数据结构、自定义属性设置、动态调整按钮高度以及边框设置。提供完整源码下载,包括 GoodsViewGroup、GoodsViewGroupItem、attrs.xml、colors.xml、正常与选中状态的drawable资源以及示例代码。
摘要由CSDN通过智能技术生成

本文是针对之前Android 仿淘宝选中商品不同尺寸的按钮组(一)存在的一些问题进行修改与完善,希望各位能再次给予意见

GoodsViewGroup修改与完善有以下几点:

一、这里就只支持TextView模式,以为在Button模式下,padding设置比其默认的更小时是看不到作用的,所以做了修整

二、添加GoodsViewGroupItem里面有key跟value两个字段,用户在这里可以自行修改,添加GoodsViewGroupItem比较方便管理跟数据的整理

三、在res——>values目录下添加attrs.xml属性给GoodsViewGroup自定义一些属性值,这样我们在布局文件中就可以来设置其(isSelector,normal_drawable,selected_drawable等等)

四、不然其修改按钮组里面的按钮高度,而现在是改用设置padding比较好点,设置固定高度可能你字体设置大了就会出现一些变形

五、该按钮添加支持有边框的设置,如果你想做的效果跟我的一模一样的话,你可以直接在colors.xml去修改其样色就ok了,效果资源中的drawable就是直接引用colors.xml中的颜色,或许你想去掉边框等等,你也可以设置GoodsViewGroup的属性(normal_drawable,selected_drawable)

下面还是一样先给效果图:

源码下载

1、这里先展示主角,也是上图中的按钮组控件:GoodsViewGroup.class

/**
 * Created by SoBan on 2016/8/22.
 * 这里是类似淘宝中商品尺寸按钮组
 */
public class GoodsViewGroup extends ViewGroup {

    private List<GoodsViewGroupItem> mItems = new ArrayList<>();
    private Context mContext;

    private int horInterval; //水平间隔
    private int verInterval; //垂直间隔

    private int viewWidth;   //控件的宽度
    private int viewHeight;  //控件的高度

    //按钮水平跟垂直内边距
    private int horPadding;
    private int verPadding;

    //正常样式
    private float textSize;
    private int bgResoureNor;
    private int textColorNor;

    //选中的样式
    private int bgResoureSel;
    private int textColorSel;

    private boolean isSelector; //是否做选择之后的效果

    public GoodsViewGroup(Context context) {
        this(context, null);
    }

    public GoodsViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        getResources().getColor(R.color.goods_item_text_normal);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet set) {
        mContext = context;
        TypedArray attrs = mContext.obtainStyledAttributes(set, R.styleable.GoodsViewGroup);
        isSelector = attrs.getBoolean(R.styleable.GoodsViewGroup_isSelector, true);
        textSize = attrs.getDimensionPixelSize(R.styleable.GoodsViewGroup_itemTextSize, 0);
        if (textSize == 0) {
            textSize = getResources().getDimensionPixelSize(R.dimen.goods_item_text);//14sp
        }
        horInterval = attrs.getDimensionPixelSize(R.styleable.GoodsViewGroup_horInterval, 20);
        verInterval = attrs.getDimensionPixelSize(R.styleable.GoodsViewGroup_verInterval, 20);
        horPadding = attrs.getDimensionPixelSize(R.styleable.GoodsViewGroup_horPadding, 20);
        verPadding = attrs.getDimensionPixelSize(R.styleable.GoodsViewGroup_verPadding, 10);
        bgResoureNor = attrs.getResourceId(R.styleable.GoodsViewGroup_normal_drawable, R.drawable.goods_ite
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值