Android 自定義標(biāo)題欄的實(shí)例詳解
Android 自定義標(biāo)題欄的實(shí)例詳解
開發(fā) Android APP 經(jīng)常會(huì)用到自定義標(biāo)題欄,而有多級(jí)頁(yè)面的情況下還需要給自定義標(biāo)題欄傳遞數(shù)據(jù)。
本文要點(diǎn):
自定義標(biāo)題填充不完整
自定義標(biāo)題欄返回按鈕的點(diǎn)擊事件
一、代碼
這里先介紹一下流程:
1. 創(chuàng)建一個(gè)標(biāo)題欄布局文件 mytitlebar.xml
2. 在style.xml中創(chuàng)建 mytitlestyle 主題
3. 創(chuàng)建類 CustomTitleBar
4. 在需要自定義標(biāo)題欄的Activity的OnCreate方法中實(shí)例化 CustomTitleBar
5. 在 AndroidManifest.xml 對(duì)使用了自定義標(biāo)題欄的Activity定義主題
1.定義一個(gè)自定義的標(biāo)題欄布局 mytitlebar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/re_title" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp" //定義自定義標(biāo)題欄的高度
android:background="@color/start_background"
android:orientation="horizontal">
<ImageButton
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:id="@+id/bt_back"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/left_back"
android:background="@color/touming"/>
<TextView
android:id="@+id/mytitle"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"http://使文字在整個(gè)標(biāo)題欄的中間
android:textColor="#fff"
android:textSize="20dp" />
</RelativeLayout >
2.在 style.xml 中創(chuàng)建 mytitlestyle 主題
<resources>
<!-- 自定義標(biāo)題欄 parent="android:Theme" 這個(gè)屬性必須寫 -->
<style name="mytitlestyle" parent="android:Theme">
<!-- 設(shè)置高度,和 mytitlebar.xml中保持一致 -->
<item name="android:windowTitleSize">50dp</item>
<!-- 設(shè)置內(nèi)填充為0 使自定義標(biāo)題填充整個(gè)標(biāo)題欄,否則左右兩邊有空隙 -->
<item name="android:padding">0dp</item>
</style>
</resources>
3.創(chuàng)建類 CustomTitleBar
public class CustomTitleBar {
private Activity mActivity;
//不要使用 static 因?yàn)橛腥?jí)頁(yè)面返回時(shí)會(huì)報(bào)錯(cuò)
/**
* @param activity
* @param title
* @see [自定義標(biāo)題欄]
*/
public void getTitleBar(Activity activity, String title) {
mActivity = activity;
activity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
//指定自定義標(biāo)題欄的布局文件
activity.setContentView(R.layout.mytitlebar);
activity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.mytitlebar);
//獲取自定義標(biāo)題欄的TextView控件并設(shè)置內(nèi)容為傳遞過來的字符串
TextView textView = (TextView) activity.findViewById(R.id.mytitle);
textView.setText(title);
//設(shè)置返回按鈕的點(diǎn)擊事件
ImageButton titleBackBtn = (ImageButton) activity.findViewById(R.id.bt_back);
titleBackBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//調(diào)用系統(tǒng)的返回按鍵的點(diǎn)擊事件
mActivity.onBackPressed();
}
});
}
}
4.在需要自定義標(biāo)題欄的Activity的OnCreate方法中實(shí)例化 CustomTitleBar,這里是food頁(yè)面
public class food extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//實(shí)例化CustomTitleBar 傳遞相應(yīng)的參數(shù)
CustomTitleBar ct = new CustomTitleBar();
ct.getTitleBar(this, "美食");
setContentView(R.layout.page_food);
}
}
5.在 AndroidManifest.xml 對(duì)使用了自定義標(biāo)題欄的Activity定義主題
//省略了其余部分,android:theme="@style/mytitlestyle"這句必需寫
<activity
android:name=".food"
android:label="@string/activity_food"
android:theme="@style/mytitlestyle" />
二、總結(jié)
使用自定義標(biāo)題欄的時(shí)候,很多人會(huì)遇到填充不滿,左右兩邊有空隙以及返回按鈕點(diǎn)擊事件不響應(yīng)的問題,這里測(cè)試和總結(jié)了最為合適的方式解決。
自定義標(biāo)題欄填充不滿,網(wǎng)上有不少解決方案,有的還比較復(fù)雜,我這里直接在定義Theme時(shí)一個(gè)屬性就解決了,還比較容易理解。
自定義標(biāo)題欄返回按鈕點(diǎn)擊事件不響應(yīng)或出錯(cuò)的問題,也是測(cè)試了網(wǎng)上的很多代碼,用onBackPressed()最為方便,也有人使用finish(),其余的OnKeyDown之類的測(cè)試未通過。
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
實(shí)例詳解android studio如何導(dǎo)入.so文件的方法
通過實(shí)例給大家詳細(xì)講解了如何在android studio如何導(dǎo)入.so文件以及中間遇到的問題解決辦法,需要的讀者們可以仔細(xì)學(xué)習(xí)一下。2017-12-12
Android提高之SurfaceView與多線程的混搭實(shí)例
這篇文章主要介紹了Android提高之SurfaceView與多線程的混搭,很實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android實(shí)現(xiàn)仿360桌面懸浮清理內(nèi)存
今天給大家?guī)硪粋€(gè)仿360手機(jī)衛(wèi)士懸浮窗清理內(nèi)存的效果的教程,非常的簡(jiǎn)單實(shí)用,需要的小伙伴可以參考下2015-12-12
Android利用Document實(shí)現(xiàn)xml讀取和寫入操作
這篇文章主要為大家詳細(xì)介紹了Android利用Document實(shí)現(xiàn)xml讀取和寫入操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
flutter實(shí)現(xiàn)底部導(dǎo)航欄切換
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)底部導(dǎo)航欄切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
ProtoBuf動(dòng)態(tài)拆分Gradle?Module解析
這篇文章主要為大家介紹了ProtoBuf動(dòng)態(tài)拆分Gradle?Module解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
關(guān)于Android CountDownTimer的使用及注意事項(xiàng)
這篇文章主要介紹了關(guān)于Android CountDownTimer的使用及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
Android中BroadcastReceiver(異步接收廣播Intent)的使用
Broadcast Receiver是Android的五大組件之一,使用頻率也很高,用于異步接收廣播Intent,本文將詳細(xì)介紹,需要的朋友可以參考下2012-12-12
Android開發(fā)DataBinding基礎(chǔ)使用
這篇文章主要為大家介紹了Android開發(fā)DataBinding基礎(chǔ)使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06

