Android 自定義view實(shí)現(xiàn)TopBar效果
本文實(shí)例為大家分享了Android自定義view實(shí)現(xiàn)TopBar的具體代碼,供大家參考,具體內(nèi)容如下
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bwie.test.MainActivity">
<com.bwie.test.MyView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lt="http://schemas.android.com/apk/res-auto"
android:id="@+id/titlebar"
android:layout_width="match_parent"
android:layout_height="60dp"
lt:leftButtonText="返回"
lt:leftButtonTextColor="@android:color/white"
lt:leftButtonTextSize="8sp"
lt:buttonBgColor="#4556ec"
lt:titleText="標(biāo)題"
lt:titleColor="@android:color/white"
lt:titleSize="8sp"
lt:rightButtonText="完成"
lt:rightButtonTextColor="@android:color/white"
lt:rightButtonTextSize="8sp"
android:background="#47ea10"
android:padding="10sp"
>
</com.bwie.test.MyView>
</RelativeLayout>
自定義屬性attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Titlebar">
<attr name="leftButtonText" format="string|reference"></attr>
<attr name="leftButtonTextColor" format="color|reference"></attr>
<attr name="leftButtonTextSize" format="dimension|reference"></attr>
<attr name="leftButtonImage" format="color|reference"></attr>
<attr name="buttonBgColor" format="color"/>
<attr name="titleText" format="string|reference"></attr>
<attr name="titleColor" format="color|reference"></attr>
<attr name="titleSize" format="dimension|reference"></attr>
<attr name="rightButtonText" format="string|reference"></attr>
<attr name="rightButtonTextColor" format="color|reference"></attr>
<attr name="rightButtonTextSize" format="dimension|reference"></attr>
<attr name="rightButtonImage" format="color|reference"></attr>
</declare-styleable>
</resources>
自定義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();
}
});
}
}
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)WebView點(diǎn)擊攔截跳轉(zhuǎn)原生
這篇文章主要介紹了Android實(shí)現(xiàn)WebView點(diǎn)擊攔截跳轉(zhuǎn)原生,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup帶箭頭的圓角矩形菜單實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android仿知乎客戶端關(guān)注和取消關(guān)注的按鈕點(diǎn)擊特效實(shí)現(xiàn)思路詳解
這篇文章主要介紹了Android仿知乎客戶端關(guān)注和取消關(guān)注的按鈕點(diǎn)擊特效實(shí)現(xiàn)思路詳解的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Android入門之TableLayout應(yīng)用解析(二)
這篇文章主要介紹了Android入門之TableLayout應(yīng)用,需要的朋友可以參考下2014-08-08
Android應(yīng)用開發(fā)中實(shí)現(xiàn)apk皮膚文件換膚的思路分析
這篇文章主要介紹了Android應(yīng)用開發(fā)中實(shí)現(xiàn)apk皮膚文件換膚的思路分析,包括布局和主要的皮膚更換邏輯實(shí)現(xiàn),需要的朋友可以參考下2016-02-02
Android三種方式生成矢量圖之VectorDrawable類使用詳解
這篇文章主要介紹了Android三種方式生成矢量圖的VectorDrawable類,2014年6月26日的I/O?2014開發(fā)者大會上谷歌正式推出了Android?L,它帶來了全新的設(shè)計(jì)語言Material?Design,新的API也提供了這個類VectorDrawable2023-02-02
Android單項(xiàng)綁定MVVM項(xiàng)目模板的方法
這篇文章主要給大家介紹了關(guān)于Android單項(xiàng)綁定MVVM項(xiàng)目模板的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Android中AsyncTask的入門使用學(xué)習(xí)指南
AsyncTask異步任務(wù),用于執(zhí)行耗時任務(wù)并在UI線程中更新結(jié)果。下面這篇文章主要給大家介紹了關(guān)于Android中AsyncTask入門使用的相關(guān)資料,需要的朋友可以參考下2019-02-02
Android實(shí)現(xiàn)絢麗的自定義進(jìn)度條
進(jìn)度條是在Android項(xiàng)目中很常用的組件之一,本文將為大家詳細(xì)地介紹一下自定義進(jìn)度條的實(shí)現(xiàn)過程。感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-01-01

