x+=width+marginLeft;//将测量到的宽度添加到x中
if(row<1){
y=maxHeight+marginTop;//将测量到的高度添加进来
}
//当累加的宽度大于最大宽度时
if(x>maxWidth){
if(index != 0){//防止第一个view就大于最大宽度
row++;
}
if(width>=maxWidth-marginLeft){//如果这个view比最大宽度还要宽,那x直接设为最大宽度
x = maxWidth;
}else{
x=width+marginLeft;//否则就充值当前x
}
y+=maxHeight+marginTop;//增加行数后高度也得累加
maxHeight = 0;//重置最大高度,开始下一行的计算
}
}
}
setMeasuredDimension(maxWidth,y);//设置容器需要的宽度和高度
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int maxWidth=r-1;//获取最大宽度
int x = 0,y = 0,row = 0;
int maxHeight = 0;
for(int index = 0;index < getChildCount();index++){
View child=getChildAt(index);
if(child.getVisibility()!=GONE){
int width = child.getMeasuredWidth(),height = child.getMeasuredHeight();
if(height>maxHeight)
maxHeight = height;
x+=width+marginLeft;
if(row<1){
y=maxHeight+marginTop;
}
if(x>maxWidth){
if(index!=0)
row++;
if(width>=maxWidth-marginLeft){
x=maxWidth;
}else {
x=width+marginLeft;
}
y+=maxHeight+marginTop;
maxHeight=0;
}
child.layout(x-width,y-height,x,y);
}
}
}
private void init() {
mOnCheckedChangeListener=new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
checkedList.add(compoundButton.getId());
}else {
checkedList.remove(compoundButton.getId());
}
}
};
}
@Override
public void addView(View child) {
if(child instanceof CheckBox){
CheckBox checkBox = (CheckBox) child;
if(checkBox.isChecked()){
setCheckIdList(checkBox.getId());
}
checkBox.setOnCheckedChangeListener(mOnCheckedChangeListener);
}
super.addView(child);
}
private void setCheckIdList(@IdRes int id) {
checkedList.add(id);
}
public void clearChecked(){
checkedList.clear();
}
private void removeChecked(@IdRes int id){
checkedList.remove(id);
}
}
attr.xml文件:
<declare-styleable name="CheckBoxGroup">
<attr name="childMarginLeft" format="dimension"/>
<attr name="childMarginTop" format="dimension"/>
</declare-styleable>
public class ScreenCheckBox extends CheckBox {
public ScreenCheckBox(Context context) {
super(context);
setTextStyle();
setOnCheckChangeLiistener();
}
private void setOnCheckChangeLiistener(){
this.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
compoundButton.setText("√" + compoundButton.getText());
compoundButton.setTextColor(Color.parseColor("#d52c34"));
}else if (compoundButton.getText().toString().contains("√")){
compoundButton.setText(getText().subSequence(1, compoundButton.getText().length()));
compoundButton.setTextColor(Color.parseColor("#000000"));
}
}
});
}
private void setTextStyle(){
setBackgroundResource(R.drawable.bg_checkbox);
setButtonTintMode(PorterDuff.Mode.CLEAR);
if (isChecked()) {
setText("√" + getText());
setTextColor(Color.parseColor("#d52c34"));
}
}
public String getCheckBoxText(){
if (getText().toString().contains("√")){
return getText().toString().substring(1);
}else
return getText().toString();
}
}
drawable文件下新建3个xml文件bg\_checkbox,bg\_check\_red,bg\_check\_gray:
bg_checkbox:
<item android:drawable="@drawable/bg_check_red" android:state_checked="true"/>
<item android:drawable="@drawable/bg_check_gray" android:state_checked="false"/>
bg_check_red:
<corners android:radius="3dp" />
<stroke
android:width="1dp"
android:color="#d52c3d" />
`接下来是字母索引:`
`自定义View 继承View,重写onDraw方法`
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 获取焦点改变背景颜色.
int height = getHeight();// 获取对应高度
int width = getWidth(); // 获取对应宽度
int singleHeight = height / b.length;// 获取每一个字母的高度
for (int i = 0; i < b.length; i++) {
paint.setColor(Color.rgb(33, 65, 98));
// paint.setColor(Color.WHITE);
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
paint.setTextSize(30);
// 选中的状态
if (i == choose) {
// paint.setColor(Color.parseColor(“#3399ff”));
paint.setColor(getResources().getColor(R.color.maincolor));
paint.setFakeBoldText(true);
}
// x坐标等于中间-字符串宽度的一半.
float xPos = width / 2 - paint.measureText(b[i]) / 2;
float yPos = singleHeight * i + singleHeight;
canvas.drawText(b[i], xPos, yPos, paint);
paint.reset();// 重置画笔
}
}
public boolean dispatchTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float y = event.getY();// 点击y坐标
final int oldChoose = choose;
final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
switch (action) {
case MotionEvent.ACTION_UP:
setBackgroundDrawable(new ColorDrawable(0x00000000));
choose = -1;//
invalidate();
if (mTextDialog != null) {
mTextDialog.setVisibility(View.INVISIBLE);
}
break;
default:
// setBackgroundResource(R.drawable.sidebar_background);
if (oldChoose != c) {
写在最后
在技术领域内,没有任何一门课程可以让你学完后一劳永逸,再好的课程也只能是“师傅领进门,修行靠个人”。“学无止境”这句话,在任何技术领域,都不只是良好的习惯,更是程序员和工程师们不被时代淘汰、获得更好机会和发展的必要前提。
如果你觉得自己学习效率低,缺乏正确的指导,可以一起学习交流!
加入我们吧!群内有许多来自一线的技术大牛,也有在小厂或外包公司奋斗的码农,我们致力打造一个平等,高质量的Android交流圈子,不一定能短期就让每个人的技术突飞猛进,但从长远来说,眼光,格局,长远发展的方向才是最重要的。
35岁中年危机大多是因为被短期的利益牵着走,过早压榨掉了价值,如果能一开始就树立一个正确的长远的职业规划。35岁后的你只会比周围的人更值钱。