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;
}
}

使用

   <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() { }
});

展示效果

继承RelativeLayout 自定义布局-LMLPHP

04-30 04:56