Android自定义通用标题栏CustomTitleBar
程序员文章站
2023-11-14 14:25:46
本文实例为大家分享了android自定义通用标题栏的具体代码,供大家参考,具体内容如下/p>
1自定义一个public_titlebar.xml文件
<&...
本文实例为大家分享了android自定义通用标题栏的具体代码,供大家参考,具体内容如下/p>
1自定义一个public_titlebar.xml文件
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rootview" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <imageview android:id="@+id/ivleft" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/z"/> <linearlayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"> <textview android:id="@+id/tvtitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="tvtitle"/> <textview android:id="@+id/tvright" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="tvright"/> </linearlayout> </linearlayout>
2.在values文件夹下新建一个attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="customertitlebar"> <attr name="left_image" format="reference"></attr> <attr name="center_text" format="string"></attr> <attr name="center_text_color" format="color"></attr> <attr name="center_text_size" format="dimension"></attr> </declare-styleable> </resources>
3.自定义customertitlebar类继承linearlayout,统一页面标题栏,项目中包括接口回调
public class customertitlebar extends linearlayout { private linearlayout rootview; private imageview ivleft; private textview tvtitle; private textview tvright; //3.声明回调对象 private customerclick leftclick; /** * @param context */ //在代码中直接new一个custom view实例的时候,会调用第一个构造函数 public customertitlebar(context context) { this(context,null); } //在xml布局文件中调用custom view的时候,会调用第二个构造函数 public customertitlebar(context context,attributeset attrs) { this(context, attrs,-1); } //在xml布局文件中调用custom view,并且custom view标签中还有自定义属性时,这里调用的还是第二个构造函数. public customertitlebar(context context,attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(context); initattrs(context,attrs); initlister(); } private void init(context context) { rootview= (linearlayout) view.inflate(context,r.layout.layout_customer_title_bar,this); ivleft=rootview.findviewbyid(r.id.ivleft); tvtitle=rootview.findviewbyid(r.id.tvtitle); tvright=rootview.findviewbyid(r.id.tvright); } private void initattrs(context context, attributeset attrs) { typedarray typedarray=context.obtainstyledattributes(attrs,r.styleable.customertitlebar); drawable drawable=typedarray.getdrawable(r.styleable.customertitlebar_left_image); if (drawable!=null){ ivleft.setimagedrawable(drawable); } charsequence text = typedarray.gettext(r.styleable.customertitlebar_center_text); if (!textutils.isempty(text)) { tvtitle.settext(text); } int color = typedarray.getcolor(r.styleable.customertitlebar_center_text_color, -1); if (color != -1) { tvtitle.settextcolor(color); } float dimension = typedarray.getdimension(r.styleable.customertitlebar_center_text_size, 0f); tvtitle.settextsize(dimension); } private void initlister() { ivleft.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { //5.使用接口回调 if (leftclick!=null){ leftclick.onlefclick(v); } } }); } //4、提供回调对象的set方法 public void setleftclick(customerclick leftclick) { this.leftclick = leftclick; } //1.定义回调接口 interface customerclick{ void onlefclick(view view); } }
4.在布局文件中的引用
<com.cn.jyx.customertitlebar.customertitlebar android:id="@+id/cttitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" customer:center_text="吐泡泡" customer:center_text_color="#ff00ff" customer:center_text_size="20sp" />
5.在activity中的用法
public class mainactivity extends appcompatactivity { private customertitlebar cttitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); cttitle=findviewbyid(r.id.cttitle); //6、使用 cttitle.setleftclick(new customertitlebar.customerclick() { @override public void onlefclick(view view) { toast.maketext(mainactivity.this,"吐泡泡",toast.length_long).show(); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。