Android自定義頂部標(biāo)題欄
本文實(shí)例為大家分享了Android自定義頂部標(biāo)題欄展示的具體代碼,供大家參考,具體內(nèi)容如下
思路及實(shí)現(xiàn)步驟
1.定義標(biāo)題欄布局
2.自定義TitleActivity控制標(biāo)題欄按鈕監(jiān)聽
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)文章
Android實(shí)現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫(kù))
本篇文章主要介紹了Android實(shí)現(xiàn)與Apache Tomcat服務(wù)器數(shù)據(jù)交互(MySql數(shù)據(jù)庫(kù)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
最近在做一個(gè) Android 項(xiàng)目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡(jiǎn)單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來(lái)與大家分享2013-01-01Android 中HttpURLConnection與HttpClient使用的簡(jiǎn)單實(shí)例
這篇文章介紹了Android 中HttpURLConnection與HttpClient使用的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-10-10Android中Service和Activity相互通信示例代碼
在android中Activity負(fù)責(zé)前臺(tái)界面展示,service負(fù)責(zé)后臺(tái)的需要長(zhǎng)期運(yùn)行的任務(wù)。下面這篇文章主要給大家介紹了關(guān)于Android中Service和Activity相互通信的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09android自定義進(jìn)度條漸變色View的實(shí)例代碼
這篇文章主要介紹了android自定義進(jìn)度條漸變色View的實(shí)例代碼,有需要的朋友可以參考一下2014-01-01Android中LayoutInflater.inflater()的正確打開方式
這篇文章主要給大家介紹了關(guān)于Android中LayoutInflater.inflater()的正確打開方式,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)播放視頻時(shí)切換全屏并隱藏狀態(tài)欄的方法,結(jié)合實(shí)例形式分析了Android視頻播放事件響應(yīng)及相關(guān)屬性設(shè)置操作技巧,需要的朋友可以參考下2017-08-08Android毛玻璃背景效果簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要介紹了Android毛玻璃背景效果簡(jiǎn)單實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-08-08Android Webview與ScrollView的滾動(dòng)兼容及留白處理的方法
本篇文章主要介紹了Android Webview與ScrollView的滾動(dòng)兼容及留白處理的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11