Android實(shí)現(xiàn)自定義標(biāo)題欄的方法
本文要講自己定義一個(gè)標(biāo)題欄,能加事件。然后可以移值到不同的手機(jī)上,基本上不用改什么,調(diào)用也很簡(jiǎn)單
在layout文件夾下,新建一個(gè)XML。名字叫做layout_title_bar.xml然后來看看布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="45.0dip"
android:background="@drawable/bg_title_bar"
android:gravity="top" >
<ImageView
android:id="@+id/title_bar_menu_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3.0dip"
android:layout_marginRight="3.0dip"
android:layout_marginTop="3.0dip"
android:gravity="center"
android:src="@drawable/ic_top_bar_category" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="@id/title_bar_menu_btn"
android:background="@drawable/ic_top_divider" />
<TextView
android:id="@+id/title_bar_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:gravity="center"
android:paddingLeft="75.0dip"
android:paddingRight="75.0dip"
android:singleLine="true"
android:text="Java學(xué)習(xí)寶典"
android:textColor="#ffffff"
android:textSize="22sp" />
</RelativeLayout>
看下效果:

接下要就是要用了,在要用到的地方:我這里是activity_main.xml文件中:
加上一句: <include layout="@layout/layout_title_bar" />這樣就行了,
然后我們要給標(biāo)題欄上的按鈕添加事件,這個(gè)更加簡(jiǎn)單了:
在MainActivity.java(對(duì)應(yīng)activity_main.xml)中,onCreate函數(shù)中添加:事件可以自己改,我這里是讓它控制左右滑動(dòng)的功能。
ImageView menuImg = (ImageView) findViewById(R.id.title_bar_menu_btn);
menuImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (!menuIsShow)
showMenu();
else {
hideMenu();
}
}
});
這樣就可以了:
我們來看看效果

這就是效果了,很簡(jiǎn)單吧,想用直接把上面的布局復(fù)制過去就OK了!
相關(guān)文章
如何自己實(shí)現(xiàn)Android View Touch事件分發(fā)流程
這篇文章主要介紹了如何自己實(shí)現(xiàn)Android View Touch事件分發(fā)流程,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android懸浮球及全局返回功能的實(shí)現(xiàn)示例
這篇文章主要介紹了Android懸浮球及全局返回功能的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Android判斷網(wǎng)絡(luò)類型的方法(2g,3g還是wifi)
這篇文章主要介紹了Android判斷網(wǎng)絡(luò)類型的方法,可實(shí)現(xiàn)判斷2g,3g還是wifi的功能,結(jié)合實(shí)例形式分析了Android針對(duì)網(wǎng)絡(luò)類型的相關(guān)判定技巧,需要的朋友可以參考下2016-02-02
Android 自定義標(biāo)題欄 顯示網(wǎng)頁加載進(jìn)度的方法實(shí)例
Android 自定義標(biāo)題欄 顯示網(wǎng)頁加載進(jìn)度的方法實(shí)例,需要的朋友可以參考一下2013-06-06
Android開發(fā)實(shí)例之登錄界面的實(shí)現(xiàn)
本文主要介紹Android 登錄界面實(shí)現(xiàn),這里主要講解類似Twitter的登錄界面的實(shí)現(xiàn),有興趣的小伙伴可以參考下2016-08-08
android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)多線程斷點(diǎn)續(xù)傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android RecycleView添加head配置封裝的實(shí)例
這篇文章主要介紹了Android RecycleView添加head配置封裝的實(shí)例的相關(guān)資料,這里提供實(shí)例幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08
http請(qǐng)求繞過Filter的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了http請(qǐng)求繞過Filter的實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06

