Android自定義頂部標(biāo)題欄
本文實(shí)例為大家分享了Android自定義頂部標(biāo)題欄展示的具體代碼,供大家參考,具體內(nèi)容如下

思路及實(shí)現(xiàn)步驟
1.定義標(biāo)題欄布局
2.自定義TitleActivity控制標(biāo)題欄按鈕監(jiān)聽(tīng)
3.在TitleActivity中實(shí)現(xiàn)標(biāo)題欄以下內(nèi)容切換
首先定義標(biāo)題欄
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_titlebar"
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="#ed4255" >
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:gravity="center_horizontal|center"
android:singleLine="true"
android:text="標(biāo)題欄"
android:textColor="#ffffffff"
android:textSize="20dp" />
<Button
android:id="@+id/button_backward"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@drawable/title_button_selector"
android:drawableLeft="@drawable/back_arrow"
android:drawablePadding="6dp"
android:ellipsize="end"
android:gravity="center"
android:onClick="onClick"
android:paddingLeft="5dp"
android:singleLine="true"
android:text="返回"
android:textColor="#ffffffff"
android:textSize="18dp"
android:visibility="invisible" />
<Button
android:id="@+id/button_forward"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@drawable/title_button_selector"
android:drawablePadding="6dp"
android:ellipsize="end"
android:gravity="center"
android:onClick="onClick"
android:paddingLeft="5dp"
android:singleLine="true"
android:text="提交"
android:textColor="#ffffffff"
android:textSize="18dp"
android:visibility="invisible" />
</RelativeLayout>
定義控制標(biāo)題欄按鈕和標(biāo)題欄以下內(nèi)容的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Title -->
<include layout="@layout/layout_titlebar" />
<FrameLayout
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
</FrameLayout>
</LinearLayout>
注:此處使用 <include> 標(biāo)簽引入標(biāo)題欄,且下方有定義一個(gè)空的FrameLayout的布局。
定義TitleActivity控制按鈕及布局
package org.gaochun.widget;
import org.gaochun.ui.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author gao_chun
* 自定義標(biāo)題欄
*/
public class TitleActivity extends Activity implements OnClickListener{
//private RelativeLayout mLayoutTitleBar;
private TextView mTitleTextView;
private Button mBackwardbButton;
private Button mForwardButton;
private FrameLayout mContentLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupViews(); //加載 activity_title 布局 ,并獲取標(biāo)題及兩側(cè)按鈕
}
private void setupViews() {
super.setContentView(R.layout.activity_title);
mTitleTextView = (TextView) findViewById(R.id.text_title);
mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
mBackwardbButton = (Button) findViewById(R.id.button_backward);
mForwardButton = (Button) findViewById(R.id.button_forward);
}
/**
* 是否顯示返回按鈕
* @param backwardResid 文字
* @param show true則顯示
*/
protected void showBackwardView(int backwardResid, boolean show) {
if (mBackwardbButton != null) {
if (show) {
mBackwardbButton.setText(backwardResid);
mBackwardbButton.setVisibility(View.VISIBLE);
} else {
mBackwardbButton.setVisibility(View.INVISIBLE);
}
} // else ignored
}
/**
* 提供是否顯示提交按鈕
* @param forwardResId 文字
* @param show true則顯示
*/
protected void showForwardView(int forwardResId, boolean show) {
if (mForwardButton != null) {
if (show) {
mForwardButton.setVisibility(View.VISIBLE);
mForwardButton.setText(forwardResId);
} else {
mForwardButton.setVisibility(View.INVISIBLE);
}
} // else ignored
}
/**
* 返回按鈕點(diǎn)擊后觸發(fā)
* @param backwardView
*/
protected void onBackward(View backwardView) {
Toast.makeText(this, "點(diǎn)擊返回,可在此處調(diào)用finish()", Toast.LENGTH_LONG).show();
//finish();
}
/**
* 提交按鈕點(diǎn)擊后觸發(fā)
* @param forwardView
*/
protected void onForward(View forwardView) {
Toast.makeText(this, "點(diǎn)擊提交", Toast.LENGTH_LONG).show();
}
//設(shè)置標(biāo)題內(nèi)容
@Override
public void setTitle(int titleId) {
mTitleTextView.setText(titleId);
}
//設(shè)置標(biāo)題內(nèi)容
@Override
public void setTitle(CharSequence title) {
mTitleTextView.setText(title);
}
//設(shè)置標(biāo)題文字顏色
@Override
public void setTitleColor(int textColor) {
mTitleTextView.setTextColor(textColor);
}
//取出FrameLayout并調(diào)用父類removeAllViews()方法
@Override
public void setContentView(int layoutResID) {
mContentLayout.removeAllViews();
View.inflate(this, layoutResID, mContentLayout);
onContentChanged();
}
@Override
public void setContentView(View view) {
mContentLayout.removeAllViews();
mContentLayout.addView(view);
onContentChanged();
}
/* (non-Javadoc)
* @see android.app.Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
*/
@Override
public void setContentView(View view, LayoutParams params) {
mContentLayout.removeAllViews();
mContentLayout.addView(view, params);
onContentChanged();
}
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
* 按鈕點(diǎn)擊調(diào)用的方法
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_backward:
onBackward(v);
break;
case R.id.button_forward:
onForward(v);
break;
default:
break;
}
}
}
MainActivity中調(diào)用時(shí)直接 extends TitleActivity 使用之前在TitleActivity中定義的方法

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin編寫(xiě)Android適配器Adapter
這篇文章主要為大家詳細(xì)介紹了Kotlin編寫(xiě)Android適配器Adapter的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
Android gradle打包并自動(dòng)上傳的方法
本篇文章主要介紹了Android gradle打包并自動(dòng)上傳的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Android中Permission權(quán)限機(jī)制的具體使用
這篇文章主要介紹了Android中Permission權(quán)限機(jī)制的具體使用,本文講解了權(quán)限級(jí)別 protection level、ICC(inter-component communication)權(quán)限保護(hù)等內(nèi)容,需要的朋友可以參考下2015-04-04
Android自定義View實(shí)現(xiàn)比賽時(shí)間閃動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)比賽時(shí)間閃動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
解析Android點(diǎn)擊事件分發(fā)機(jī)制
本篇文章主要介紹了解析Android點(diǎn)擊事件分發(fā)機(jī)制,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Android開(kāi)發(fā)Retrofit源碼分析
這篇文章主要為大家介紹了Android開(kāi)發(fā)Retrofit源碼分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法
這篇文章主要介紹了Android GridView實(shí)現(xiàn)滾動(dòng)到指定位置的方法,本文介紹了4個(gè)相關(guān)的方法,分別對(duì)它們做了講解,需要的朋友可以參考下2015-06-06
android開(kāi)發(fā)教程之間隔執(zhí)行程序(android計(jì)時(shí)器)
android開(kāi)發(fā)中有些情況需要隔一段時(shí)間去執(zhí)行某個(gè)操作一次或者是每隔一段時(shí)間久執(zhí)行某個(gè)操作,下面是實(shí)現(xiàn)方法2014-02-02
Android實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色的變化實(shí)例代碼詳解
今天介紹一下,我在項(xiàng)目開(kāi)發(fā)過(guò)程中,實(shí)現(xiàn)狀態(tài)欄和虛擬按鍵背景顏色變化的方法,實(shí)現(xiàn)方式是,通過(guò)隱藏系統(tǒng)的狀態(tài)欄和虛擬按鍵的背景,實(shí)現(xiàn)圖片和背景顯示到狀態(tài)欄和虛擬按鍵下方,需要的朋友可以參考下2019-05-05

