Android基于Toolbar實(shí)現(xiàn)頂部標(biāo)題欄及后退鍵
最近設(shè)計(jì)安卓里面有個(gè)標(biāo)題欄,里面有個(gè)后退鍵,可以完成后退之類的功能。
好,剛好可以用Toolbar去實(shí)現(xiàn)

上代碼:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="設(shè)置"
android:textSize="22sp" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
這里需要引用styles.xml在里面加樣式
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Base.Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
最后主程序:
package action.sun.com.testtoobar1;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //設(shè)置全屏*/
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//一定要加,為了去掉本身的標(biāo)題文字
toolbar.setTitle("");
//初始化toolbar
setSupportActionBar(toolbar);
//左邊的小箭頭(注意需要在setSupportActionBar(toolbar)之后才有效果)
toolbar.setNavigationIcon(R.mipmap.back);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Toast.makeText(MainActivity.this, "選擇了菜單", Toast.LENGTH_SHORT).show();
//初始化右邊的菜單選項(xiàng)
//getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Toast.makeText(MainActivity.this, "選擇了后退按鈕="+id, Toast.LENGTH_SHORT).show();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
}
到此代碼完畢,就能夠完成需要上圖實(shí)現(xiàn)的效果了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Android應(yīng)用中DialogFragment的基本用法
Android App中建議使用DialogFragment作為對話框的容器,DialogFragment類提供了創(chuàng)建對話框并管理其外觀需要的所有控件,本文主要內(nèi)容便為詳解Android應(yīng)用中DialogFragment的基本用法,而不再需要調(diào)用Dialog的方法需要的朋友可以參考下2016-05-05
Android組件banner實(shí)現(xiàn)左右滑屏效果
這篇文章主要為大家詳細(xì)介紹了Android組件banner實(shí)現(xiàn)左右滑屏效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android實(shí)現(xiàn)自定義手勢和識別手勢的功能
這篇文章主要介紹了Android實(shí)現(xiàn)自定義手勢和識別手勢的功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Android 優(yōu)雅的實(shí)現(xiàn)通用格式化編輯
這篇文章主要介紹了Android 優(yōu)雅的實(shí)現(xiàn)通用格式化編輯,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android實(shí)現(xiàn)RecyclerView下拉刷新效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)RecyclerView下拉刷新效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android 自定義view實(shí)現(xiàn)進(jìn)度條加載效果實(shí)例代碼
這篇文章主要介紹了Android 自定義view實(shí)現(xiàn)進(jìn)度條加載效果實(shí)例代碼,需要的朋友可以參考下2017-08-08
android開發(fā)基礎(chǔ)教程—文件存儲功能實(shí)現(xiàn)
文件存儲功能在實(shí)現(xiàn)數(shù)據(jù)讀寫時(shí)會頻繁使用到,接下來介紹文件存儲功能的實(shí)現(xiàn),感興趣的朋友可以了解下2013-01-01

