public class TopBar extends RelativeLayout { private Button left; private final Button right; private final TextView text; setLeftAndRightButton listenr; private SearchView serch; public void setLeftAndRightButton(setLeftAndRightButton listenr){ this.listenr=listenr; } public interface setLeftAndRightButton{ void onleft(); void onright(); } public TopBar(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.topbar,this); left = findViewById(R.id.left); serch = findViewById(R.id.ser); right = findViewById(R.id.right); text = findViewById(R.id.textView); serch.setIconified(false); serch.setIconifiedByDefault(false); serch.onActionViewExpanded(); left.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { listenr.onleft(); } }); right.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { listenr.onright(); } }); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.fuTopBar); int leftback = typedArray.getResourceId(R.styleable.fuTopBar_leftBackgroud, 0); int rightback = typedArray.getResourceId(R.styleable.fuTopBar_rightBackgroud, 0); String texttitle = typedArray.getString(R.styleable.fuTopBar_titleText); float textsize = typedArray.getDimension(R.styleable.fuTopBar_titleTextSize, 0); int textcolor = typedArray.getColor(R.styleable.fuTopBar_titleTextColor, 0); int serche = typedArray.getResourceId(R.styleable.fuTopBar_search, 0); typedArray.recycle(); left.setBackgroundResource(leftback); right.setBackgroundResource(rightback); // serch.setBackgroundDrawable(serche); text.setText(texttitle); text.setTextSize(textsize); text.setTextColor(textcolor); } }
自定义TopBar
最新推荐文章于 2021-12-06 22:18:52 发布