Android custom View instance
For Android-related custom View basic knowledge, refer to Android View Custom Attributes. This blog will share with you the practices of Android custom View. We know that TitleBar is the most common one in applications, they are consistent in form, generally the left-side rollback button, intermediate description text, and right-side function button. Therefore, it is very suitable for selecting a custom View template.
Custom View-attrs
Custom View class
import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TextView;public class TitleBar extends RelativeLayout{ private Button mRightButton, mLeftButton; private TextView mCenterTextView; private int mCenterColor, mRightColor, mLeftColor; private Drawable mCenterBackground, mRightBackground, mLeftBackground; private float mCenterSize, mRightSize, mLeftSize; private String mCenterText, mRightText, mLeftText; public void setOnTitleBarClickListener(OnTitleBarClickListener mOnTitleBarClickListener) { this.mOnTitleBarClickListener = mOnTitleBarClickListener; } private OnTitleBarClickListener mOnTitleBarClickListener; public TitleBar(Context context, AttributeSet attrs) { super(context, attrs); final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleBarCustom); final int N = a.getIndexCount(); for (int i = 0; i < N; i++) { int attr = a.getIndex(i); switch (attr) { case R.styleable.TitleBarCustom_center_color: mCenterColor = a.getColor(attr, -1); break; case R.styleable.TitleBarCustom_center_size: mCenterSize = a.getDimension(attr, 0); break; case R.styleable.TitleBarCustom_center_text: mCenterText = a.getString(attr); break; case R.styleable.TitleBarCustom_center_ground: mCenterBackground = a.getDrawable(attr); break; case R.styleable.TitleBarCustom_left_color: mLeftColor = a.getColor(attr, -1); break; case R.styleable.TitleBarCustom_left_size: mLeftSize = a.getDimension(attr, 0); break; case R.styleable.TitleBarCustom_left_text: mLeftText = a.getString(attr); break; case R.styleable.TitleBarCustom_left_ground: mLeftBackground = a.getDrawable(attr); break; case R.styleable.TitleBarCustom_right_color: mRightColor = a.getColor(attr, -1); break; case R.styleable.TitleBarCustom_right_size: mRightSize = a.getDimension(attr, 0); break; case R.styleable.TitleBarCustom_right_text: mRightText = a.getString(attr); break; case R.styleable.TitleBarCustom_right_ground: mRightBackground = a.getDrawable(attr); break; } } mRightButton = new Button(context); mRightButton.setText(mRightText); mRightButton.setTextSize(mRightSize); mRightButton.setTextColor(mRightColor); mRightButton.setBackgroundDrawable(mRightBackground); RelativeLayout.LayoutParams rightParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); rightParams.addRule(ALIGN_PARENT_RIGHT, TRUE); rightParams.addRule(CENTER_VERTICAL, TRUE); rightParams.setMargins(0, 0, 10, 0); mRightButton.setLayoutParams(rightParams); addView(mRightButton); mRightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mOnTitleBarClickListener != null) { mOnTitleBarClickListener.rightButtonClick(); } } }); mLeftButton = new Button(context); mLeftButton.setText(mLeftText); mLeftButton.setTextSize(mLeftSize); mLeftButton.setTextColor(mLeftColor); mLeftButton.setBackgroundDrawable(mLeftBackground); RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); leftParams.addRule(ALIGN_PARENT_LEFT, TRUE); leftParams.addRule(CENTER_VERTICAL, TRUE); leftParams.setMargins(10, 0, 0, 0); mLeftButton.setLayoutParams(leftParams); addView(mLeftButton); mLeftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (mOnTitleBarClickListener != null) { mOnTitleBarClickListener.leftButtonClick(); } } }); mCenterTextView = new TextView(context); mCenterTextView.setText(mCenterText); mCenterTextView.setTextSize(mCenterSize); mCenterTextView.setTextColor(mCenterColor); mCenterTextView.setBackgroundDrawable(mCenterBackground); RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); centerParams.addRule(CENTER_IN_PARENT, TRUE); leftParams.setMargins(10, 0, 10, 0); mCenterTextView.setLayoutParams(centerParams); addView(mCenterTextView); a.recycle(); } @SuppressWarnings("unused") public void setRightButtonVisiablity(boolean isVisiable) { if (mRightButton == null) return; if (isVisiable) { mRightButton.setVisibility(View.VISIBLE); } else { mRightButton.setVisibility(View.GONE); } } @SuppressWarnings("unused") public void setLeftButtonVisiablity(boolean isVisiable) { if (mLeftButton == null) return; if (isVisiable) { mLeftButton.setVisibility(View.VISIBLE); } else { mLeftButton.setVisibility(View.GONE); } } @SuppressWarnings("unused") public void setCenterTextVisiablity(boolean isVisiable) { if (mCenterTextView == null) return; if (isVisiable) { mCenterTextView.setVisibility(View.VISIBLE); } else { mCenterTextView.setVisibility(View.GONE); } } public interface OnTitleBarClickListener { void leftButtonClick(); void rightButtonClick(); }}
Use custom View
First, define in the UI
Then use
public class SixActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_title); initView(); } private void initView() { final TitleBar titleBar = (TitleBar) findViewById(R.id.title_bar); if (titleBar != null) { titleBar.setOnTitleBarClickListener(new OnTitleBarClickListener() { @Override public void leftButtonClick() { Toast.makeText(SixActivity.this, "Left button click!", Toast.LENGTH_LONG).show(); } @Override public void rightButtonClick() { Toast.makeText(SixActivity.this, "Right button click!", Toast.LENGTH_LONG).show(); } }); } }}
This completes the custom View of the entire TitleBar. Although it is rough, the basic model has come out. This is a typical example of combining custom views, other custom views will be updated later.