自定義 ActionBar

1 . 創(chuàng)建布局 actionbar_custom.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="60dp"
              android:background="#8ab3f4"
              android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="#ffffff"
        android:text="ListDemo"/>
</LinearLayout>

2 . activity 中設(shè)置 actionbar

ActionBar actionBar =  getSupportActionBar();
        View actionbarView = LayoutInflater.from(this).inflate(R.layout.actionbar_custom,null);
        ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL |Gravity.CENTER_HORIZONTAL;
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setCustomView(actionbarView, layoutParams);
        Toolbar parent =(Toolbar) actionbarView.getParent();
        parent.setContentInsetsAbsolute(0,0);

3 . actionbar顯示返回按鈕

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish(); // back button
            return true;
    }
    return super.onOptionsItemSelected(item);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,094評論 25 709
  • ¥開啟¥ 【iAPP實現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,336評論 0 17
  • 原文地址:http://www.android100.org/html/201606/06/241682.html...
    AFinalStone閱讀 1,303評論 0 1
  • 經(jīng)過幾個月團(tuán)隊小伙伴的共同努力,產(chǎn)品已經(jīng)順利上線了,接下來就需要給客戶進(jìn)行培訓(xùn)了,如何在培訓(xùn)的過程中讓客戶看到我們...
    華思語閱讀 528評論 2 7
  • 春日落下了秋的葉就如遇見你的那個夏天 日子在陽光的浮影中跳躍你在花叢中追尋蝴蝶的歌唱 你閃亮的毛發(fā)伴著你永遠(yuǎn)的歡笑...
    作者平安閱讀 264評論 1 1

友情鏈接更多精彩內(nèi)容