欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 自定义控件之TopBar

程序员文章站 2022-03-04 11:58:50
...


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

    1,自定义TopBar 的属性 

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


 

 <declare-styleable name="MyTopBar">
        <attr name="title" format="string" />
        <attr name="titleTextSize" format="dimension" />
        <attr name="titleTextColor" format="color" />

        <attr name="leftTitle" format="string" />
        <attr name="leftTitleTextColor" format="color" />
        <attr name="LeftBackground" format="reference|color" />

        <attr name="rightTitle" format="string" />
        <attr name="rightTitleTextColor" format="color" />
        <attr name="rightBackground" format="reference|color" />
    </declare-styleable>

   

     这里定义了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 使用:

<com.xxx.TopBar
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        app:title = "title"
        app:titleTextSize = "12dp"
        app:titleTextColor = "#000000"
        app:leftTitle = "left"
        app:leftTitleTextColor = "#ffffff"
        app:LeftBackground = "#000000"

        app:rightTitle = "right"
        app:rightTitleTextColor = "#ffffff"
        app:rightBackground = "#000000"
        >
    </com.xxx.TopBar>



一个简单TopBar实现。



相关标签: 控件