Android標(biāo)題欄中添加返回按鈕功能
標(biāo)題欄中的返回按鈕在實(shí)際使用中用的比較多,今天就來講講我在項(xiàng)目開發(fā)中的使用經(jīng)歷,話不多說,還是直接上源碼,上源碼是最給力的。
一、 編寫自定義類
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>
三 、 在需要調(diào)用的activity中調(diào)用
public class InformationActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); CustomTitle.getCustomTitle(this, "個(gè)人信息"); 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,完成上述幾個(gè)步驟,就可以了。
以上所述是小編給大家介紹的Android標(biāo)題欄中添加返回按鈕功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Android實(shí)現(xiàn)類似ios滑動(dòng)按鈕
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)類似ios滑動(dòng)按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08Android實(shí)現(xiàn)ListView左右滑動(dòng)刪除和編輯
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)ListView左右滑動(dòng)刪除和編輯的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05Android沉浸式狀態(tài)欄微技巧(帶你真正理解沉浸式模式)
因?yàn)锳ndroid官方從來沒有給出過沉浸式狀態(tài)欄這樣的命名,只有沉浸式模式(Immersive Mode)這種說法.下面通過本文給大家介紹Android沉浸式狀態(tài)欄微技巧,需要的朋友參考下2016-12-12Android Mms之:聯(lián)系人管理的應(yīng)用分析
本篇文章是對Android中的聯(lián)系人管理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致
看IOS上的應(yīng)用,應(yīng)用中狀態(tài)欄的顏色總能與應(yīng)用標(biāo)題欄顏色保持一致,用戶體驗(yàn)很不錯(cuò),對于這種效果怎么實(shí)現(xiàn)的呢?下面小編給大家分享android自定義狀態(tài)欄顏色與應(yīng)用標(biāo)題欄顏色一致的實(shí)現(xiàn)方法,一起看看吧2016-09-09android中知道圖片name時(shí)獲取圖片的簡單方法
android中知道圖片name時(shí)獲取圖片的簡單方法,需要的朋友可以參考一下2013-05-05Android編程之利用服務(wù)實(shí)現(xiàn)電話監(jiān)聽的方法
這篇文章主要介紹了Android編程之利用服務(wù)實(shí)現(xiàn)電話監(jiān)聽的方法,較為詳細(xì)的分析了Android基于服務(wù)實(shí)現(xiàn)針對電話監(jiān)聽的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-11-11Android自定義View實(shí)現(xiàn)體重表盤詳解流程
對于安卓程序員來說,自定義view簡直不要太重要,畢竟有很多功能,譬如圓形頭像這些,用單純的原生非常難以實(shí)現(xiàn),而用自定義view,簡直分分鐘2021-11-11安卓開發(fā)之FragmentPagerAdapter和FragmentStatePagerAdapter詳解
這篇文章主要介紹了安卓開發(fā)之FragmentPagerAdapter和FragmentStatePagerAdapter詳解的相關(guān)資料,需要的朋友可以參考下2022-08-08