t70次列车,henhenlula,艳鬼广播剧
标题栏中的返回按钮在实际使用中用的比较多,今天就来讲讲我在项目开发中的使用经历,话不多说,还是直接上源码,上源码是最给力的。
一、 编写自定义类
public class customtitle { private static activity mactivity; public static void getcustomtitle(activity activity, string title) { mactivity = activity; mactivity.requestwindowfeature(window.feature_custom_title); mactivity.setcontentview(r.layout.custom_title); mactivity.getwindow().setfeatureint(window.feature_custom_title, r.layout.custom_title); textview textview = (textview) activity.findviewbyid(r.id.head_center_text); textview.settext(title); button titlebackbtn = (button) activity.findviewbyid(r.id.titlebackbtn); titlebackbtn.setonclicklistener(new onclicklistener() { public void onclick(view v) { log.d("title back","key down"); mactivity.finish(); } }); } }
二 、 xml资源,在layout中定义custom_title
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <button android:id="@+id/titlebackbtn" android:layout_width="50dp" android:layout_height="wrap_content" android:gravity="center" android:layout_alignparentright="true" android:background="@android:drawable/ic_menu_revert"/> <textview android:id="@+id/head_center_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="" android:textsize="25sp" android:textcolor="#ffffff" /> </relativelayout>
三 、 在需要调用的activity中调用
public class informationactivity extends activity{ @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); customtitle.getcustomtitle(this, "个人信息"); setcontentview(r.layout.informationactivity); ....................... } }
四 、 在res/values/style.xml中添加style定义
<style name="mycustomtheme" parent="android:theme"> <item name="android:windowtitlebackgroundstyle">@style/titlebarbackground</item> <item name="android:windowtitlesize">50dp</item> </style>
五 、 在androidmanifest.xml中对informationactivity添加支持
android:name="com.xxx.informationactivity" android:theme="@style/mycustomtheme" android:screenorientation="landscape" />
ok,完成上述几个步骤,就可以了。
以上所述是小编给大家介绍的android标题栏中添加返回按钮功能,希望对大家有所帮助
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android apk 项目一键打包并上传到蒲公英的实现方法
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
android 限制某个操作每天只能操作指定的次数(示例代码详解)
Android 集成 google 登录并获取性别等隐私信息的实现代码
网友评论