Android中BaseActivity自定義標(biāo)題欄
再做一個(gè)項(xiàng)目的時(shí)候,要求標(biāo)題欄的標(biāo)題再中間,樣式,字體大小都要自定義。左邊一個(gè)返回按鈕,一個(gè)關(guān)閉按鈕,右邊定義一個(gè)提交按鈕,有時(shí)候顯示有時(shí)候隱藏。因?yàn)樵膖itle標(biāo)題是再左邊的,然后去給Titlebar設(shè)置自定義View的時(shí)候,也會不盡人意,標(biāo)題不是再正中間的,標(biāo)題欄太高等問題。
我們要求的是這樣的,右邊的按鈕可以顯示或者隱藏。
于是就決定自己寫一個(gè)BaseActivity,所有的都去繼承這個(gè)基類,然后自己去定義標(biāo)題欄的樣式就可以就可以了。
下面來講一下這個(gè)界面是怎么實(shí)現(xiàn)的:
首先定義一個(gè)類BaseActivity:
public class BaseActivity extends AppCompatActivity implements View.OnClickListener{
private TextView mTitleTextView;//標(biāo)題
private TextView close_tv;//
protected TextView commint_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//將界面加入到棧中,方便管理
MyApplication.getInstance().addActivity(this);
initViews();
}
private void initViews() {
super.setContentView(R.layout.activity_abstract_title);
mTitleTextView = (TextView) findViewById(R.id.action_bar_title_tv);
mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
close_tv = ((TextView) findViewById(R.id.action_bar_close_tv));
ImageView back_ic = (ImageView) findViewById(R.id.action_bar_back_iv);
commint_tv = (TextView) findViewById(R.id.action_bar_comint_tv);
back_ic.setOnClickListener(this);
mTitleTextView.setOnClickListener(this);
}
// 返回鍵返回事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == keyCode) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public boolean onTouchEvent(MotionEvent event) {
if(null != this.getCurrentFocus()){
/**
* 點(diǎn)擊空白位置 隱藏軟鍵盤
*/
InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
}
return super .onTouchEvent(event);
}
/**
* 顯示關(guān)閉按鈕
*/
public void showCloseBT(){
if (close_tv!=null){
close_tv.setVisibility(View.VISIBLE);
close_tv.setOnClickListener(this);
}
}
/**
* 顯示提交按鈕按鈕
*/
public void showCommintBT(String s){
if (commint_tv!=null){
commint_tv.setVisibility(View.VISIBLE);
commint_tv.setOnClickListener(this);
commint_tv.setText(s);
}
}
/**
* 返回按鈕點(diǎn)擊后觸發(fā)
*/
protected void onLeftBackward() {
onBackPressed();
}
/**
* 右邊提交按鈕點(diǎn)擊后觸發(fā)
*/
protected void onRightForward() {
}
/**
* 提交關(guān)閉按鈕點(diǎn)擊后觸發(fā)
*/
protected void onLeftCloseword(){
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("tabpos", 2);
startActivity(intent);
finish();
}
//設(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);
}
//點(diǎn)擊標(biāo)題時(shí)出發(fā)的事件操作
public void onTitle() {
}
//取出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();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.action_bar_back_iv:
onLeftBackward();
break;
case R.id.action_bar_comint_tv:
onRightForward();
break;
case R.id.action_bar_close_tv:
onLeftCloseword();
case R.id.action_bar_title_tv:
onTitle();
default:
break;
}
}
}
這樣的話別的Activity去繼承BaseActivity的時(shí)候,只需要去設(shè)置是否顯示某個(gè)按鈕即可,標(biāo)題欄各個(gè)按鈕的點(diǎn)擊事件不需要去設(shè)置,直接重寫
onLeftBackward();onRightForward();onRightForward();onTitle();
然后對應(yīng)各自的方法就可以了。
下面給出布局文件activity_abstract_title.xml:
<?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/actionbar_layout" />
<FrameLayout
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
</FrameLayout>
</LinearLayout>
actionbar_layout.xml文件
<?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="40dp"
android:background="#2B2B2B">
<TextView
android:id="@+id/action_bar_title_tv"
android:layout_width="180dp"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:gravity="center_horizontal|center"
android:lines="1"
android:textColor="#fff"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:layout_centerInParent="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:textSize="18sp" />
<ImageView
android:contentDescription="@string/cancel"
android:id="@+id/action_bar_back_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="10dp"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#fff"
android:src="@drawable/arrow_left" />
<TextView
android:layout_toEndOf="@id/action_bar_back_iv"
android:text="@string/action_bar_close"
android:id="@+id/action_bar_close_tv"
android:textColor="#fff"
android:visibility="invisible"
android:textSize="15sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"/>
<TextView
android:visibility="invisible"
android:padding="10dp"
android:layout_alignParentEnd="true"
android:text="@string/action_bar_commint"
android:id="@+id/action_bar_comint_tv"
android:textSize="15sp"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginEnd="3dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</RelativeLayout>
下面是一個(gè)簡單的應(yīng)用:
public class DemoActivity extends MyBaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("APPBaseActivity");//設(shè)置標(biāo)題
showCloseBT();//顯示關(guān)閉按鈕,默認(rèn)時(shí)隱藏的
}
//如果返回按鈕有其他操作的話可以重寫
@Override
protected void onLeftBackward() {
super.onLeftBackward();
//里面寫事件就可以
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter?Getx中的put和lazyPut函數(shù)使用案例解析
這篇文章主要為大家介紹了Flutter?Getx中的put和lazyPut函數(shù)使用案例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法,涉及Android針對按鈕元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2015-11-11
Android Studio中快捷鍵實(shí)現(xiàn)try catch等功能包含代碼塊的實(shí)現(xiàn)方法
這篇文章主要介紹了 Android Studio中快捷鍵實(shí)現(xiàn)try catch等功能包含代碼塊的實(shí)現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09
Android SlidingDrawer 抽屜效果的實(shí)現(xiàn)
本篇文章小編為大家介紹,Android SlidingDrawer 抽屜效果的實(shí)現(xiàn)。需要的朋友參考下2013-04-04
Jetpack Compose按鈕組件使用實(shí)例詳細(xì)講解
這篇文章主要介紹了Jetpack Compose按鈕組件使用實(shí)例,按鈕組件Button是用戶和系統(tǒng)交互的重要組件之一,它按照Material Design風(fēng)格實(shí)現(xiàn),我們先看下Button的參數(shù)列表,通過參數(shù)列表了解下Button的整體功能2023-04-04
Android實(shí)現(xiàn)讀取掃碼槍內(nèi)容(條形碼)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)讀取掃碼槍內(nèi)容、條形碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

