Android 自定义控件之TopBar


    自定义控件学习之简单实现自己的TopBar

    1,自定义TopBar 的属性 

     首先创建自己的attrs.xml ,自定义自己的属性


 

 
        
        
        

        
        
        

        
        
        
    

   

     这里定义了title,leftTitle ,rightTitle。


2,在构造方法中获取自定义属性

public class TopBar extends RelativeLayout {

    private Button leftButton ,rightButton ;
    private TextView titleTextView ;

    private int leftTextColor ;
    private Drawable leftDrawable ;
    private String leftText ;


    private int rightTextColor ;
    private Drawable rightDrawable ;
    private String rightText ;


    private float titleTextSize ;

    private int titleTextColor ;

    private String title ;

    private LayoutParams leftLayoutParams ,middleLayoutParams,rightLayoutParams;
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyTopBar);

          leftTextColor  = typedArray.getColor(R.styleable.MyTopBar_leftTitleTextColor,0);
          leftDrawable = typedArray.getDrawable(R.styleable.MyTopBar_LeftBackground);
          leftText  = typedArray.getString(R.styleable.MyTopBar_leftTitle);

          rightTextColor  = typedArray.getColor(R.styleable.MyTopBar_rightTitleTextColor,0);
        rightDrawable = typedArray.getDrawable(R.styleable.MyTopBar_rightBackground);
        rightText  = typedArray.getString(R.styleable.MyTopBar_rightTitle);

        titleTextSize = typedArray.getDimension(R.styleable.MyTopBar_titleTextSize,0);
        titleTextColor = typedArray.getColor(R.styleable.MyTopBar_titleTextColor,0);
        title = typedArray.getString(R.styleable.MyTopBar_title);
        typedArray.recycle();
        leftButton = new Button(context);
        rightButton = new Button(context);
        titleTextView = new TextView(context);

        leftButton.setTextColor(leftTextColor);
        leftButton.setBackground(leftDrawable);
        leftButton.setText(leftText);

        rightButton.setTextColor(rightTextColor);
        rightButton.setBackground(rightDrawable);
        rightButton.setText(rightText);

        titleTextView.setTextColor(titleTextColor);
        titleTextView.setTextSize(titleTextSize);
        titleTextView.setText(title);
        titleTextView.setGravity(Gravity.CENTER);
        setBackgroundColor(0xFFF59563);

        leftLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        leftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        addView(leftButton,leftLayoutParams);

        rightLayoutParams =  new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(rightButton,rightLayoutParams);

        middleLayoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        middleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);

        addView(titleTextView,middleLayoutParams);
    }
}


xml 使用:


    



一个简单TopBar实现。



你可能感兴趣的:(android自定义View)