Android ToolBar使用

來源:互聯網
上載者:User

標籤:context   inf   port   點擊事件   main   idg   strong   log   android   

ToolBar簡介

ToolBar是Android 5.0推出的一個新的導航控制項用於取代之前的ActionBar,由於其高度的可定製性、靈活性、具有Material Design風格等優點,越來越多的應用也用上了ToolBar,比如常用的知乎軟體其頂部導覽列正是使用ToolBar。

 

ToolBar基本使用

更改主題

為了能夠正常使用ToolBar,我們需要隱藏原來的ActionBar,這個可以在主題中修改,在res/values/styles.xml中做出如下修改:

 

 

在布局檔案添加這個控制項,代碼如下:

 

 1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     xmlns:app="http://schemas.android.com/apk/res-auto" 4     xmlns:tools="http://schemas.android.com/tools" 5     android:layout_width="match_parent" 6     android:layout_height="match_parent" 7     tools:context="com.contentprovide.liuliu.toolbardemo.MainActivity"> 8  9     <android.support.v7.widget.Toolbar10         android:layout_width="match_parent"11         android:layout_height="wrap_content">12 13         <TextView14             android:layout_width="wrap_content"15             android:layout_height="wrap_content"16             android:text="標題列" />17 18     </android.support.v7.widget.Toolbar>19 20 </android.support.constraint.ConstraintLayout>

 

 

 

可以看到我在ToolBar裡面放了一個TextView控制項,這個ToolBar就相當於一個ViewGroup。在它內部放的view就可以自訂標題列樣式了.

運行程式,效果:

 

 

要進一步完善標題列只需要在裡面添加控制項,也可以加上點擊事件讓功能更完善

 

Android ToolBar使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.