本文是针对之前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