Android標(biāo)題欄中添加返回按鈕功能
標(biāo)題欄中的返回按鈕在實(shí)際使用中用的比較多,今天就來(lái)講講我在項(xiàng)目開(kāi)發(fā)中的使用經(jīng)歷,話不多說(shuō),還是直接上源碼,上源碼是最給力的。
一、 編寫(xiě)自定義類
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中對(duì)InformationActivity添加支持
android:name="com.xxx.InformationActivity"
android:theme="@style/MyCustomTheme"
android:screenOrientation="landscape" />
OK,完成上述幾個(gè)步驟,就可以了。
以上所述是小編給大家介紹的Android標(biāo)題欄中添加返回按鈕功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
Android自定義ViewGroup實(shí)現(xiàn)受邊界限制的滾動(dòng)操作(3)
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)受邊界限制的滾動(dòng)操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
100行Android代碼輕松實(shí)現(xiàn)帶動(dòng)畫(huà)柱狀圖
這篇文章主要教大家通過(guò)100行Android代碼輕松實(shí)現(xiàn)帶動(dòng)畫(huà)柱狀圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
android教程viewpager自動(dòng)循環(huán)和手動(dòng)循環(huán)
這篇文章主要介紹了android的viewpager自動(dòng)循環(huán)和手動(dòng)循環(huán)示例,需要的朋友可以參考下2014-02-02
Android 標(biāo)準(zhǔn)Intent的使用詳解
這篇文章主要介紹了Android 標(biāo)準(zhǔn)Intent的使用詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
利用百度地圖Android sdk高仿微信發(fā)送位置功能及遇到的問(wèn)題
這篇文章給大家介紹了利用百度地圖Android sdk高仿微信發(fā)送位置功能,在實(shí)現(xiàn)此功能的時(shí)候遇到點(diǎn)小問(wèn)題,下面小編給大家列出來(lái),需要的朋友參考下吧2017-12-12
Android實(shí)現(xiàn)左滑關(guān)閉窗口
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)左滑關(guān)閉窗口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android AutoCompleteTextView自動(dòng)提示文本框?qū)嵗a
這篇文章主要介紹了Android AutoCompleteTextView自動(dòng)提示文本框?qū)嵗a的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

