public class HomeToolbarView extends RelativeLayout { TextView tvTitle; public HomeToolbarView(Context context) { super(context); addView(context); } public HomeToolbarView(Context context, AttributeSet attrs) { super(context, attrs); addView(context); } public HomeToolbarView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); addView(context); } public void addView(Context mContext){ ImageView imageViewMenu=new ImageView(mContext); ImageView imageViewMessage=new ImageView(mContext); tvTitle=new TextView(mContext); addView(imageViewMenu); addView(imageViewMessage); addView(tvTitle); RelativeLayout.LayoutParams menuLayoutParams = (RelativeLayout.LayoutParams) imageViewMenu.getLayoutParams(); RelativeLayout.LayoutParams messageLayoutParams = (RelativeLayout.LayoutParams) imageViewMessage.getLayoutParams(); RelativeLayout.LayoutParams titleLayoutParams = (RelativeLayout.LayoutParams) tvTitle.getLayoutParams(); menuLayoutParams.width=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth); menuLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth); menuLayoutParams.setMargins(getResources().getDimensionPixelOffset(R.dimen.common_margin) ,0,0,0); menuLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE); menuLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE); titleLayoutParams.width= LayoutParams.MATCH_PARENT; titleLayoutParams.height=LayoutParams.MATCH_PARENT; titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE); messageLayoutParams.width=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth); messageLayoutParams.height=getResources().getDimensionPixelOffset(R.dimen.home_toolbar_image_width_heigth); messageLayoutParams.setMargins(0 ,0,getResources().getDimensionPixelOffset(R.dimen.common_margin),0); messageLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE); messageLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE); imageViewMenu.setBackground(ContextCompat.getDrawable(mContext,R.drawable.ico_toolbar_left_menu)); imageViewMessage.setBackground(ContextCompat.getDrawable(mContext,R.drawable.ico_toolbar_left_message)); tvTitle.setText(mContext.getString(R.string.app_name)); tvTitle.setTextSize(getResources().getDimension(R.dimen.textsize_8)); tvTitle.setTextColor(ContextCompat.getColor(mContext,R.color.common_blue)); tvTitle.setGravity(Gravity.CENTER); imageViewMenu.setOnClickListener(new OnClickListener() { public void onClick(View v) { listener.leftClick(); } }); imageViewMessage.setOnClickListener(new OnClickListener() { public void onClick(View v) { listener.rightClick(); } }); imageViewMenu.setLayoutParams(menuLayoutParams); imageViewMessage.setLayoutParams(messageLayoutParams); tvTitle.setLayoutParams(titleLayoutParams); } public void setTitle(String title){ tvTitle.setText(title); } //自定義的頂部ActionBar的點擊監聽; private HomeToolbarClickListener listener; //點擊事件的監聽接口 public interface HomeToolbarClickListener { void leftClick(); void rightClick(); } //提供activity調用的方法,相似於Button類的setOnClickListener(OnClickListener listener) //傳入具體實現方法 public void setOnTopbarClickListener(HomeToolbarClickListener listener){ this.listener=listener; } }
使用html
<com.freexiaoyu.app.widget.HomeToolbarView android:id="@id/toolbar_home" android:layout_width="match_parent" android:background="@color/white" android:layout_height="48.0dp" android:orientation="vertical"/>
activity @BindView(R.id.toolbar_home) HomeToolbarView toolbar_home; toolbar_home.setTitle(getString(R.string.app_name)); toolbar_home.setOnTopbarClickListener(new HomeToolbarView.HomeToolbarClickListener() { @Override public void leftClick() { if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { mDrawerLayout.closeDrawer(Gravity.LEFT); } else { mDrawerLayout.openDrawer(Gravity.LEFT); } } @Override public void rightClick() { } });
展現效果android