本文实例为大家分享了AndroID自定义view实现topbar的具体代码,供大家参考,具体内容如下
布局文件
<?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="标题" 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); //左侧的按钮 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,getResources().getdisplayMetrics())); //右侧的按钮 mRightbuttonText = typedArray.getString(R.styleable.Titlebar_rightbuttonText); mRightbuttonTextcolor = typedArray.getcolor(R.styleable.Titlebar_rightbuttonTextcolor,color.GRAY); mRightbuttonSize = typedArray.getDimension(R.styleable.Titlebar_rightbuttonTextSize,getResources().getdisplayMetrics())); mRightbuttonImage = typedArray.getDrawable(R.styleable.Titlebar_rightbuttonImage); typedArray.recycle();//回收 /*调用方法*/ initVIEw(context); } public MyVIEw(Context context,AttributeSet attrs,int defStyleAttr) { this(context,defStyleAttr,0); } public MyVIEw(Context context,int defStyleAttr,int defStyleRes) { super(context,defStyleRes); } /*构建按钮*/ private voID initVIEw(Context context) { if(mleftbuttonImage == null & mleftbuttonText != null){ // 当用户没有设置左侧按钮图片并设置了左侧的按钮文本属性时--添加左侧文本按钮 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){ // 添加左侧图片按钮 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){ // 添加中间标题 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){ // 当用户没有设置右侧按钮图片并设置了左侧的按钮文本属性时--添加右侧文本按钮 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){ // 添加右侧图片按钮 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); } } /*监听事件*/ public interface OnbuttonClickListener{ voID onleftClick(); voID onRightClick(); } /*点击事件*/ 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方法的代码调用自定义的类和点击事件
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); /*点击事件*/ MyvIEw.setonbuttonClickListener(new MyVIEw.OnbuttonClickListener() { @OverrIDe public voID onleftClick() { Toast.makeText(MainActivity.this,"左侧按钮被点击了",Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onRightClick() { Toast.makeText(MainActivity.this,"右侧按钮被点击了",Toast.LENGTH_SHORT).show(); } }); } }
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android 自定义view实现TopBar效果全部内容,希望文章能够帮你解决Android 自定义view实现TopBar效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)