本文實例為大家分享了Android自定義頂部標題欄展示的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)公司服務(wù)項目包括那曲網(wǎng)站建設(shè)、那曲網(wǎng)站制作、那曲網(wǎng)頁制作以及那曲網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,那曲網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到那曲省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
思路及實現(xiàn)步驟
1.定義標題欄布局
2.自定義TitleActivity控制標題欄按鈕監(jiān)聽
3.在TitleActivity中實現(xiàn)標題欄以下內(nèi)容切換
首先定義標題欄
<?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="標題欄" 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>
定義控制標題欄按鈕和標題欄以下內(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> 標簽引入標題欄,且下方有定義一個空的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 * 自定義標題欄 */ 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 布局 ,并獲取標題及兩側(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 } /** * 返回按鈕點擊后觸發(fā) * @param backwardView */ protected void onBackward(View backwardView) { Toast.makeText(this, "點擊返回,可在此處調(diào)用finish()", Toast.LENGTH_LONG).show(); //finish(); } /** * 提交按鈕點擊后觸發(fā) * @param forwardView */ protected void onForward(View forwardView) { Toast.makeText(this, "點擊提交", Toast.LENGTH_LONG).show(); } //設(shè)置標題內(nèi)容 @Override public void setTitle(int titleId) { mTitleTextView.setText(titleId); } //設(shè)置標題內(nèi)容 @Override public void setTitle(CharSequence title) { mTitleTextView.setText(title); } //設(shè)置標題文字顏色 @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à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)用時直接 extends TitleActivity 使用之前在TitleActivity中定義的方法
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當前名稱:Android自定義頂部標題欄
分享URL:http://aaarwkj.com/article10/ijhhgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、云服務(wù)器、響應(yīng)式網(wǎng)站、移動網(wǎng)站建設(shè)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)