自定义头部布局

package com.example.administrator.simulation_weekone;

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

/**
 * Created by Administrator on 2018/6/9.
 */

public class HeadText extends LinearLayout implements View.OnClickListener{

    private TextView title_text;
    private Button left_btn;
    private Button right_btn;
    private Custom_HeadText cc;
    private TextView textView;
    private int context=0;
    private int width;

    public HeadText(Context context) {
        super(context);
        initView(context,null);
    }

    public HeadText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View inflate = inflate(context, R.layout.headtext_layout, this);
        cc = inflate.findViewById(R.id.custom_headtext);
        title_text = inflate.findViewById(R.id.title_text);
        left_btn = inflate.findViewById(R.id.left_btn);
        right_btn = inflate.findViewById(R.id.right_btn);
        width = AppUtils.screenWidth(getContext());
        left_btn.setOnClickListener(this);
        right_btn.setOnClickListener(this);
        title_text.setOnClickListener(this);
        Custom_HeadText custom_headText = new Custom_HeadText(getContext());

        if(attrs==null){
            return;
        }
        initAttrs(context,attrs);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HeadText);

        title_text.setText(typedArray.getString(R.styleable.HeadText_title_text));
        left_btn.setText(typedArray.getString(R.styleable.HeadText_left_btn));
        right_btn.setText(typedArray.getString(R.styleable.HeadText_right_btn));

        title_text.setTextSize(typedArray.getDimension(R.styleable.HeadText_title_text_size,16));
        left_btn.setTextSize(typedArray.getDimension(R.styleable.HeadText_left_btn_size,16));
        right_btn.setTextSize(typedArray.getDimension(R.styleable.HeadText_right_btn_size,16));

        title_text.setTextColor(typedArray.getColor(R.styleable.HeadText_title_text_color, Color.BLACK));
        left_btn.setTextColor(typedArray.getColor(R.styleable.HeadText_left_btn_color,Color.BLACK));
        right_btn.setTextColor(typedArray.getColor(R.styleable.HeadText_right_btn_color,Color.BLACK));

        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
            left_btn.setBackground(typedArray.getDrawable(R.styleable.HeadText_left_btn_bg_color));
            right_btn.setBackground(typedArray.getDrawable(R.styleable.HeadText_right_btn_bg_color));
        }else{
            left_btn.setBackgroundDrawable(typedArray.getDrawable(R.styleable.HeadText_left_btn_bg_color));
            right_btn.setBackgroundDrawable(typedArray.getDrawable(R.styleable.HeadText_right_btn_bg_color));
        }

    }


    @Override
    public void onClick(View v) {
        context++;

            if(v.equals(left_btn)){

                textView = new TextView(v.getContext());
                    textView.setWidth(width/2);
                    textView.setHeight(100);
                    textView.setText(""+context);
                    textView.setGravity(Gravity.CENTER);
                    textView.setTextColor(Color.BLUE);
                    textView.setBackgroundColor(Color.YELLOW);
                    cc.addView(textView);

            }else if(v.equals(right_btn)){

            }else if(v.equals(title_text)){
               cc.removeAllViews();
               context=0;
            }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值