Android 自定义控件 自定义属性

    xiaoxiao2021-04-12  44

    三步. 颜色相同的地方,互相对应.

    1. 自定义View

    public class CommonToolBar extends Toolbar {

    private boolean ifShowBack;//是否显示返回按钮 public CommonToolBar(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CommonToolBar); ifShowBack = a.getBoolean(R.styleable.CommonToolBar_ifShowBack,true);//第二部定义的attr,会自动生成CommonToolBar_ifShowBack if (ifShowBack) { setNavigationIcon(R.mipmap.icon_back); } a.recycle(); }}2.attrs.xml中定义attrs<resources> <declare-styleable name="CommonToolBar"> <attr name="ifShowBack" format="boolean"/> </declare-styleable> </resources>3.布局文件xml中顶级节点增加: xmlns:frame="http://schemas.android.com/apk/res-auto"自定义View增加:frame:ifShowBack="false"注意: 如果是TextView的文字大小,记得TypedValue.COMPLEX_UNIT_SP, TypedValue.COMPLEX_UNIT_PX, TypedValue.COMPLEX_UNIT_DPcenterTitleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,type.getDimension(R.styleable.CommonToolBar_centerBtnTextSize,13));

    转载请注明原文地址: https://ju.6miu.com/read-667733.html

    最新回复(0)