這篇文章給大家介紹Android中怎么通過自定義view實(shí)現(xiàn)TopBar效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站制作、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)清水河,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
布局文件
自定義屬性attrs.xml文件
自定義View的Class類
public class MyView extends RelativeLayout{ private String mLeftButtonText; private int mLeftButtonTextColor; private float mLeftButtonSize; private Drawable mLeftButtonImage; private String mTitleButtonText; private int mTitleButtonTextColor; private float mTitleButtonSize; private String mRightButtonText; private int mRightButtonTextColor; private float mRightButtonSize; private Drawable mRightButtonImage; private TextView mRightTextView; private TextView titleTextView; private ImageView mLeftButton; private TextView mLeftTextView; private ImageView mRightButton; int buttonBgColor; public MyView(Context context) { this(context,null); } public MyView(Context context, AttributeSet attrs) { this(context, attrs,0); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Titlebar); buttonBgColor = typedArray.getColor(R.styleable.Titlebar_buttonBgColor,Color.BLUE); //左側(cè)的按鈕 mLeftButtonText = typedArray.getString(R.styleable.Titlebar_leftButtonText); mLeftButtonTextColor = typedArray.getColor(R.styleable.Titlebar_leftButtonTextColor, Color.GRAY); mLeftButtonSize = typedArray.getDimension(R.styleable.Titlebar_leftButtonTextSize, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); mLeftButtonImage = typedArray.getDrawable(R.styleable.Titlebar_leftButtonImage); //中間的按鈕 mTitleButtonText = typedArray.getString(R.styleable.Titlebar_titleText); mTitleButtonTextColor = typedArray.getColor(R.styleable.Titlebar_titleColor, Color.GRAY); mTitleButtonSize = typedArray.getDimension(R.styleable.Titlebar_titleSize, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); //右側(cè)的按鈕 mRightButtonText = typedArray.getString(R.styleable.Titlebar_rightButtonText); mRightButtonTextColor = typedArray.getColor(R.styleable.Titlebar_rightButtonTextColor, Color.GRAY); mRightButtonSize = typedArray.getDimension(R.styleable.Titlebar_rightButtonTextSize, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); mRightButtonImage = typedArray.getDrawable(R.styleable.Titlebar_rightButtonImage); typedArray.recycle();//回收 /*調(diào)用方法*/ initView(context); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr,0); } public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } /*構(gòu)建按鈕*/ private void initView(Context context) { if(mLeftButtonImage == null & mLeftButtonText != null){ // 當(dāng)用戶沒有設(shè)置左側(cè)按鈕圖片并設(shè)置了左側(cè)的按鈕文本屬性時--添加左側(cè)文本按鈕 mLeftTextView = new TextView(context); mLeftTextView.setText(mLeftButtonText); mLeftTextView.setTextColor(mLeftButtonTextColor); mLeftTextView.setTextSize(mLeftButtonSize); mLeftTextView.setBackgroundColor(buttonBgColor); RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); leftParams.addRule(RelativeLayout.CENTER_VERTICAL); addView(mLeftTextView, leftParams); }else if(mLeftButtonImage != null){ // 添加左側(cè)圖片按鈕 RelativeLayout.LayoutParams leftParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); leftParams.addRule(RelativeLayout.CENTER_VERTICAL); mLeftButton = new ImageView(context); mLeftButton.setImageDrawable(mLeftButtonImage); addView(mLeftButton, leftParams); } if(mTitleButtonText!=null){ // 添加中間標(biāo)題 titleTextView = new TextView(context); titleTextView.setText(mTitleButtonText); titleTextView.setTextColor(mTitleButtonTextColor); titleTextView.setTextSize(mTitleButtonSize); RelativeLayout.LayoutParams titleTextViewParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); titleTextViewParams.addRule(RelativeLayout.CENTER_IN_PARENT); addView(titleTextView,titleTextViewParams); } if(mRightButtonImage == null & mRightButtonText != null){ // 當(dāng)用戶沒有設(shè)置右側(cè)按鈕圖片并設(shè)置了左側(cè)的按鈕文本屬性時--添加右側(cè)文本按鈕 mRightTextView = new TextView(context); mRightTextView.setText(mRightButtonText); mRightTextView.setTextColor(mRightButtonTextColor); mRightTextView.setTextSize(mRightButtonSize); mRightTextView.setBackgroundColor(buttonBgColor); RelativeLayout.LayoutParams rightParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); rightParams.addRule(RelativeLayout.CENTER_VERTICAL); addView(mRightTextView,rightParams); }else if(mRightButtonImage != null){ // 添加右側(cè)圖片按鈕 RelativeLayout.LayoutParams rightParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); rightParams.addRule(RelativeLayout.CENTER_VERTICAL); mRightButton = new ImageView(context); mRightButton.setImageDrawable(mRightButtonImage); addView(mRightButton, rightParams); } } /*監(jiān)聽事件*/ public interface OnButtonClickListener{ void onLeftClick(); void onRightClick(); } /*點(diǎn)擊事件*/ public void setOnButtonClickListener(final OnButtonClickListener onButtonClickListener) { if(onButtonClickListener !=null){ if(mLeftTextView != null){ mLeftTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onButtonClickListener.onLeftClick(); } }); } /*按鈕*/ if(mLeftButton != null){ mLeftButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onButtonClickListener.onLeftClick(); } }); } if(mRightTextView != null){ mRightTextView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onButtonClickListener.onRightClick(); } }); } /*按鈕*/ if(mRightButton != null){ mRightButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { onButtonClickListener.onRightClick(); } }); } } }
Main方法的代碼調(diào)用自定義的類和點(diǎn)擊事件
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*找到控件*/ MyView Myview = (MyView) findViewById(R.id.titlebar); /*點(diǎn)擊事件*/ Myview.setOnButtonClickListener(new MyView.OnButtonClickListener() { @Override public void onLeftClick() { Toast.makeText(MainActivity.this,"左側(cè)按鈕被點(diǎn)擊了",Toast.LENGTH_SHORT).show(); } @Override public void onRightClick() { Toast.makeText(MainActivity.this,"右側(cè)按鈕被點(diǎn)擊了",Toast.LENGTH_SHORT).show(); } }); } }
關(guān)于Android中怎么通過自定義view實(shí)現(xiàn)TopBar效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。