自定义View

public class TitleView extends LinearLayout implements View.OnClickListener{

    private TypedArray typedArray;
    private String btnleft;
    private String btnright;
    private String btntitle;
    private View inflate;
    private Button leftid;
    private Button rightid;
    private TextView titleid;

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

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

    public TitleView(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TitleView, 0, 0);
        btnleft = typedArray.getString(R.styleable.TitleView_leftbtn);
        btnright = typedArray.getString(R.styleable.TitleView_rightbtn);
        btntitle = typedArray.getString(R.styleable.TitleView_titlebtn);
        inflate = inflate(context, R.layout.title_layout, this);
        leftid = inflate.findViewById(R.id.left_btn);
        rightid = inflate.findViewById(R.id.right_btn);
        titleid = inflate.findViewById(R.id.title_tv);
        leftid.setOnClickListener(this);
        rightid.setOnClickListener(this);
        titleid.setOnClickListener(this);
        leftid.setText(btnleft);
        rightid.setText(btnright);
        titleid.setText(btntitle);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.left_btn:
                onClick.onleft();
                break;
            case R.id.right_btn:
                onClick.onright();
                break;
            case R.id.title_tv:
                onClick.ontitle();
                break;
        }
    }
    onClick onClick;
    public interface onClick{
        void onleft();
        void onright();
        void ontitle();
    }
    public void setOnchlick(onClick onchlick){
        this.onClick=onchlick;
    }
}
 
<resources>
    <declare-styleable name="TitleView">
        <attr name="leftbtn" format="string"/>
        <attr name="rightbtn" format="string"/>
        <attr name="titlebtn" format="string"/>
    </declare-styleable>
</resources>
titleView = findViewById(R.id.main_title);
flowss = findViewById(R.id.main_flow);
titleView.setOnchlick(new TitleView.onClick() {
    @Override
    public void onleft() {
        Toast.makeText(MainActivity.this,"加一",Toast.LENGTH_SHORT).show();
        TextView textView=new TextView(MainActivity.this);
        textView.setText("加一个");
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(20);
        textView.setBackgroundColor(Color.RED);
        textView.setWidth(flowss.getWidth()/2);
        textView.setHeight(30);
        flowss.addView(textView);
    }

    @Override
    public void onright() {
        boolean b;
       if (flowss.getChildAt(0)!=null){
           b=true;
       }else{
           b=false;
       }
       if (b){
           flowss.removeViewAt(0);
       }
        flowss.removeViewAt(0);
    }

    @Override
    public void ontitle() {
        flowss.removeAllViews();
    }
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值