复用自定义布局TitleBar

【第一种方案:addView】

TitleBar布局文件




    

        
    

    

    

        

        
    

BaseActivity:
public class BaseActivity extends AppCompatActivity {

    private View titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void finish(View view) {
        finish();
    }

    public void right(View view) {
    }

    public void addTitleBar(String title) {
        addTitleBar(title, null, 0);
    }

    public void addTitleBar(String title, String rightText) {
        addTitleBar(title, rightText, 0);
    }

    public void addTitleBar(String title, int rightIcon) {
        addTitleBar(title, null, rightIcon);
    }

    private void addTitleBar(String title, String rightText, int rightIcon) {
        if (titleBar == null) {
            titleBar = LayoutInflater.from(this).inflate(R.layout.view_title_bar, null);
        }
        TextView tvTitle = (TextView) titleBar.findViewById(R.id.tvTitle);
        TextView tvRight = (TextView) titleBar.findViewById(R.id.tvRight);
        ImageView ivRight = (ImageView) titleBar.findViewById(R.id.ivRight);
        tvTitle.setText(TextUtils.isEmpty(title) ? "" : title);
        tvRight.setText(TextUtils.isEmpty(rightText) ? "" : rightText);
        if (TextUtils.isEmpty(rightText) && rightIcon != 0) {
            ivRight.setVisibility(View.VISIBLE);
            ivRight.setImageResource(rightIcon);
        }
        ViewGroup rootView = (ViewGroup) this.findViewById(android.R.id.content);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        rootView.addView(titleBar, layoutParams);
    }
}
Activity中使用:
public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    public void right(View view) {
        Toast.makeText(this, "点击了提交", Toast.LENGTH_SHORT).show();
    }

    private void initView() {
        addTitleBar("注册", "提交");
//        addTitleBar("注册",R.drawable.icon_titlebar_more);
    }
}
图片资源(图片是白色的,F12看或者鼠标右键另存为):
 
  
  复用自定义布局TitleBar_第1张图片  
 
  
 
  
【第二种方案:自定义控件】

自定义控件TitleBar

public class TitleBar extends LinearLayout {

    private OnLeftClickListener mLeftClickListener;
    private OnRightClickListener mRightClickListener;
    private ViewHolder viewHolder;
    private View viewAppTitle;

    public TitleBar(Context context) {
        super(context);
        init();
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        viewAppTitle = inflater.inflate(R.layout.title_bar, null);
        this.addView(viewAppTitle, layoutParams);
        viewHolder = new ViewHolder(this);
        viewHolder.llLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (Utility.isFastDoubleClick()) {
//                    return;
//                }

                if (mLeftClickListener != null) {
                    mLeftClickListener.onLeftClick(v);
                }
            }
        });
        viewHolder.llRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (Utility.isFastDoubleClick()) {
//                    return;
//                }

                if (mRightClickListener != null) {
                    mRightClickListener.OnRightClick(v);
                }
            }
        });
        initViewsVisible(true, true, true, true);
    }

    public void initViewsVisible(boolean isLeftButtonVisile, boolean isCenterTitleVisile, boolean isRightIconVisile, boolean isRightTitleVisile) {
        // 左侧返回
        viewHolder.llLeft.setVisibility(isLeftButtonVisile ? View.VISIBLE : View.INVISIBLE);
        // 中间标题
        viewHolder.tvTitle.setVisibility(isCenterTitleVisile ? View.VISIBLE : View.INVISIBLE);
        // 右侧返回图标,文字
        if (!isRightIconVisile && !isRightTitleVisile) {
            viewHolder.llRight.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.llRight.setVisibility(View.VISIBLE);
        }
        viewHolder.ivRight.setVisibility(isRightIconVisile ? View.VISIBLE : View.GONE);
        viewHolder.tvRight.setVisibility(isRightTitleVisile ? View.VISIBLE : View.INVISIBLE);
    }

    public void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            viewHolder.tvTitle.setText(title);
        }
    }

    public void setRightTitle(String text) {
        if (!TextUtils.isEmpty(text)) {
            viewHolder.tvRight.setText(text);
        }
    }

    public void setRightIcon(int sourceID) {
        viewHolder.ivRight.setImageResource(sourceID);
    }

    public void setLeftOnclick(OnLeftClickListener mOnLeftButtonClickListener) {
        if (mOnLeftButtonClickListener != null) {
        }
    }

    public void setBackground(int color) {
        viewAppTitle.setBackgroundColor(color);
    }

    public void setOnLeftClickListener(OnLeftClickListener listen) {
        mLeftClickListener = listen;
    }

    public void setOnRightClickListener(OnRightClickListener listen) {
        mRightClickListener = listen;
    }

    public static abstract interface OnLeftClickListener {
        public abstract void onLeftClick(View v);
    }

    public static abstract interface OnRightClickListener {
        public abstract void OnRightClick(View v);
    }

    static class ViewHolder {
        LinearLayout llLeft;
        TextView tvTitle;
        RelativeLayout llRight;
        ImageView ivRight;
        TextView tvRight;

        public ViewHolder(View v) {
            llLeft = (LinearLayout) v.findViewById(R.id.llLeft);
            tvTitle = (TextView) v.findViewById(R.id.tvTitle);
            llRight = (RelativeLayout) v.findViewById(R.id.llRight);
            ivRight = (ImageView) v.findViewById(R.id.ivRight);
            tvRight = (TextView) v.findViewById(R.id.tvRight);
        }
    }
}
在布局中引用自定义控件




    

在Activity中使用:

    private void initView() {
        TitleBar titleBar = (TitleBar) findViewById(R.id.titleBar);
        titleBar.setTitle("注册");
        titleBar.setRightTitle("确认");
        titleBar.setBackground(Color.parseColor("#F34B4E"));
//        titleBar.setRightIcon(R.drawable.icon_titlebar_more);
        //返回
        titleBar.setOnLeftClickListener(new TitleBar.OnLeftClickListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }
        });
        //提交
        titleBar.setOnRightClickListener(new TitleBar.OnRightClickListener() {
            @Override
            public void OnRightClick(View v) {

            }
        });
    }

效果图:

复用自定义布局TitleBar_第2张图片


复用自定义布局TitleBar_第3张图片


你可能感兴趣的:(自定义控件)