Text1 标题,FlowLayout

一,MainActivity

package com.bwei.text1_lianxi1;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
       private MyTitleView myTitleView;
       private FlowLayout flowLayout;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化页面
        initview();
    }

    private void initview() {
        myTitleView=findViewById(R.id.mytitleview);
        flowLayout=findViewById(R.id.flowlayout);

        //设置监听
        myTitleView.setButtonOnClickListener(new MyTitleView.ButtonOnClickListener() {
            @Override
            public void titleOnClick() {
                //点击标题清空内容
                flowLayout.removeAllViews();
            }

            @Override
            public void buttonleftOnClick() {
                //点击左按钮减少view
                flowLayout.removeViewAt(0);
            }

            @Override
            public void buttonRightOnClick() {
               //点击右按钮增加view
                Button button=new Button(getApplicationContext());
                button.setWidth(100);
                button.setHeight(60);
                flowLayout.addView(button);






/*                button.setText("Fuck off");
                //FlowLayout 继承的是ViewGroup,所以这里用viewgroup
                ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);


                //设置宽度为屏幕的一半
                params.width=getWindowManager().getDefaultDisplay().getWidth()/2;
                //设置高度为屏幕的1/5
                params.height=getWindowManager().getDefaultDisplay().getHeight()/5;
                //添加布局
                flowLayout.addView(button,params);*/


            }
        });
    }


}

二,FlowLayout

package com.bwei.text1_lianxi1;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class FlowLayout extends ViewGroup {
    public FlowLayout(Context context) {
        super(context);
    }

    public FlowLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }






    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //1.测量所有孩子的宽高
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        //2。
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heigghtMode = MeasureSpec.getMode(heightMeasureSpec);


        //3.
        int width = 0;
        int height = 0;
        int lineWidth = 0;
        int lineHeight = 0;
        int totalHeight = 0;
        View childView;
        int childWidth = 0;
        int childHeight = 0;

        //4.遍历
        for (int i = 0; i <getChildCount() ; i++) {
            childView=getChildAt(i);//获得孩子组件
            childWidth = childView.getMeasuredWidth();//获得孩子宽
            width=2*childWidth;
            childHeight=childView.getMeasuredHeight();//获得孩子高
            //判断在左还是在右
            if(i%2==0){
                totalHeight+=lineHeight;
                //模为0的都是第一个,所以行宽赋0
                lineHeight=childHeight;
                lineWidth=childWidth;

            }else{
                totalHeight+=lineHeight;
                //模为1的都是第二个 所以行宽为累加
                lineWidth+=childWidth;
                lineHeight=childHeight;

            }

            if(i==getChildCount()-1){
                 totalHeight+=lineHeight;
                 height=totalHeight;
            }
        }

        //模式的判断
        width = widthMode == MeasureSpec.EXACTLY ? widthSize : width;
        height = heigghtMode == MeasureSpec.EXACTLY ? heightSize : height;
        //设置测量的宽高
        setMeasuredDimension(width,height);


    }



    @Override
    protected void onLayout(boolean bo, int left, int top, int right, int bottom) {

        int lineWidth = 0;
        int lineHeight = 0;
        int totalHeight = 0;
        View childView;
        int childWidth = 0;
        int childHeight = 0;

        //4.遍历
        for (int i = 0; i <getChildCount() ; i++) {
            childView=getChildAt(i);//获得孩子组件

            childWidth = childView.getMeasuredWidth();//获得孩子宽
            childHeight=childView.getMeasuredHeight();//获得孩子高
            //判断在左还是在右
            if(i%2==0){
                totalHeight+=lineHeight;
                //模为0的都是第一个,所以行宽赋0
                lineWidth=0;
                childViewLayout(childView,lineWidth,totalHeight,lineWidth+childWidth,totalHeight+childHeight);
                lineHeight=childHeight;
                lineWidth=childWidth;

            }else{
                totalHeight+=lineHeight;
                //模为1的都是第二个 所以行宽为累加
                childViewLayout(childView,lineWidth,totalHeight,lineWidth+childWidth,totalHeight+childHeight);
                lineWidth+=childWidth;
                lineHeight=childHeight;

            }
        }


    }


    private void childViewLayout(View childView, int l, int t, int r, int b) {
      childView.layout(l,t,r,b);


    }

}

三,MyTitlt

package com.bwei.text1_lianxi1;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import static android.widget.Toast.LENGTH_SHORT;

public class MyTitleView extends LinearLayout implements View.OnClickListener {

    private View title_viewGroup;
    private TextView texttitle;
    private Button butleft,butright;
    private ButtonOnClickListener ButtonOnClickListener;

    public MyTitleView(Context context) {
        super(context);
    }

    public MyTitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //1.加载布局
        title_viewGroup = inflate(context, R.layout.title_main, this);


        //2.获取子view
        texttitle = title_viewGroup.findViewById(R.id.tv_title);//标题id
        butleft = findViewById(R.id.but_left);
        butright=findViewById(R.id.but_right);

        //3.区获取属性
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyTitleView, 0, 0);

        //4.根据声明的属性在typeArray里取值
        String title = typedArray.getString(R.styleable.MyTitleView_tv_title);
        int title_color = typedArray.getColor(R.styleable.MyTitleView_tv_color, Color.GREEN);

        String but_left = typedArray.getString(R.styleable.MyTitleView_but_left);
        String but_right = typedArray.getString(R.styleable.MyTitleView_but_right);


        //5.赋值
        texttitle.setText(title);
        texttitle.setTextColor(title_color);

        butleft.setText(but_left);
        butright.setText(but_right);


       //6.设置点击事件
        texttitle.setOnClickListener(this);
        butleft.setOnClickListener(this);
        butright.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but_left:
                ButtonOnClickListener.buttonleftOnClick();
                break;
            case R.id.but_right:
                ButtonOnClickListener.buttonRightOnClick();
                break;
            case R.id.tv_title:
                ButtonOnClickListener.titleOnClick();
                break;

        }
    }


    //7.接口回调
    public interface ButtonOnClickListener{
        void titleOnClick();
        void buttonleftOnClick();
        void buttonRightOnClick();
    }

    //外部访问的方法
    public void setButtonOnClickListener(MyTitleView.ButtonOnClickListener buttonOnClickListener) {
        ButtonOnClickListener = buttonOnClickListener;
    }
}

四,小球

package com.bwei.text1_lianxi1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class MyView extends View {

    private Paint paint;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);



    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initPaints();

    }

    private void initPaints() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(10);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //canvas.drawCircle(100,100,30,paint);
        canvas.drawRect(300,200,400,250,paint);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    public void invalidate() {
        super.invalidate();

        paint.setColor(Color.BLUE);
    }

    @Override
    public void postInvalidate() {
        super.postInvalidate();
    }


}

布局:

1.activity_main

 <com.bwei.text1_lianxi1.MyTitleView
        android:id="@+id/mytitleview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tv_title="点此全部清空"
        app:tv_color="@color/colorAccent"
        app:but_left="-"
        app:but_right="+"
        >
    </com.bwei.text1_lianxi1.MyTitleView>

<ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <com.bwei.text1_lianxi1.FlowLayout
        android:id="@+id/flowlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </com.bwei.text1_lianxi1.FlowLayout>

    </ScrollView>
    <com.bwei.text1_lianxi1.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

2.title_main:

  <Button
        android:id="@+id/but_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="20dp"
        />

    <Button
        android:id="@+id/but_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        />

</LinearLayout>

3.values_attrs 自定义属性

<declare-styleable name="MyTitleView">
   <attr name="tv_title" format="string"/>
    <attr name="tv_color" format="color"/>



    <attr name="but_left" format="string"/>
    <attr name="but_right" format="string"/>

</declare-styleable>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值