Android新控件
- DrawerLayout 頂層容器,通常與NavigationView實現(xiàn)側(cè)滑菜單
- NavigationView 導(dǎo)航菜單
- CoordinatorLayout 一個超級強(qiáng)大的FrameLayout,主要用于根布局、與子視圖做特定交互
- AppBarLayout MD風(fēng)格滑動布局,子視圖需提供滾動行為
- CollapsingToolbarLayout 對ToolBar再次包裝
- ToolBar 工具欄、導(dǎo)航欄
- TabLayout 官方tabs組件,通常與ViewPager組合實現(xiàn)主內(nèi)容區(qū)域
- ViewPager 可以左右滑動的布局管理器
- FloatingActionButton 浮動按鈕
- TextInputLayout 包裹EditText,實現(xiàn)帶提示EditText
- SnackBar 類似Toast輕量級提示控件
- CardView 卡片式View,輕松實現(xiàn)圓角陰影效果
DrawerLayout 通常使用情況布局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<include
layout="@layout/inc_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/view_nav"
app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
通常主要布局內(nèi)容布局放在DrawerLayout中第一個位置,第二個位置放DrawerLayout中的內(nèi)容,通常與NavigationView結(jié)合實現(xiàn)抽屜式側(cè)滑菜單也可使用其他布局;
打開側(cè)滑菜單
drawerLayout.openDrawer(GravityCompat.START)
關(guān)閉側(cè)滑菜單
drawerLayout.closeDrawers()
NavigationView
app:headerLayout:NavigationView中頭部的head部分的布局,自己定義一個布局;
app:menu:指定NavigationView中的Menu布局,就是自己寫Menu中的按鈕,要放在res/menu/文件夾下;
CoordinatorLayout
一個增強(qiáng)型的FrameLayout,可以實現(xiàn)與子布局交互,比如滾動的工具欄(ToolBar)、浮動View(FloatingActionButton )為SnackBar騰出位置等;
ToolBar
ToolBar 是用來替換ActionBar的,使用ToolBar時需隱藏ActionBar;
隱藏ActionBar方法:
- 在style.xml文件下AppTheme中加入以下兩行
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
- 也可以將AppTheme的parent設(shè)置為Theme.AppCompat.Light.NoActionBar的方式
AppBarLayout
繼承LinearLayout,方向垂直,當(dāng)外部某個可滾動控件滾動時,可定制內(nèi)部控件如何移動;
通過設(shè)置子View :app:layout_scrollFlags屬性來控制子View執(zhí)行動作:
- scroll: 值設(shè)為scroll的View會跟隨滾動事件一起發(fā)生移動。就是當(dāng)指定的ScrollView發(fā)生滾動時,該View也跟隨一起滾動,就好像這個View也是屬于這個ScrollView一樣。
- enterAlways:值設(shè)為enterAlways的View,當(dāng)ScrollView往下滾動時,該View會直接往下滾動。而不用考慮ScrollView是否在滾動。
- exitUntilCollapsed:值設(shè)為exitUntilCollapsed的View,當(dāng)這個View要往上逐漸“消逝”時,會一直往上滑動,直到剩下的的高度達(dá)到它的最小高度后,再響應(yīng)ScrollView的內(nèi)部滑動事件。
- exitUntilCollapsed:值設(shè)為exitUntilCollapsed的View,當(dāng)這個View要往上逐漸“消逝”時,會一直往上滑動,直到剩下的的高度達(dá)到它的最小高度后,再響應(yīng)ScrollView的內(nèi)部滑動事件。
需要注意的是,后面兩種模式基本只有在CollapsingToolbarLayout才有用;
將AppBarLayout與滑動控件關(guān)聯(lián)起來需指定Behavior,即屬性:app:layout_behavior="@string/appbar_scrolling_view_behavior"
CollapsingToolbarLayout
CollapsingToolbarLayout是用來對Toolbar進(jìn)行再次包裝的ViewGroup,主要是用于實現(xiàn)折疊(其實就是看起來像伸縮~)的App Bar效果。它需要放在AppBarLayout布局里面,并且作為AppBarLayout的直接子View。
常見布局:
- 1
<?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"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorAccent"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="@mipmap/ic_done"/>
</android.support.design.widget.CoordinatorLayout>
- 2
<?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"
android:id="@+id/detail_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/coll_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
>
<ImageView
android:id="@+id/backdrop"
android:layout_width="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
android:layout_height="match_parent"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/card_margin">
<LinearLayout
style="@style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info"
android:textAppearance="@style/TextAppearance.AppCompat.Title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cheese_ipsum"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:clickable="true"
android:src="@mipmap/ic_discuss"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
TabLayout
Tabs選項卡,和ViewPager搭配使用可以增大界面的內(nèi)容展示量,實現(xiàn)各種個性化分類內(nèi)容展示而不互相干擾!
常用屬性:
有以下常用屬性:
- app:tabGravity="fill" 表示TabLayout中的Tabs要占滿屏幕的width;
- app:tabSelectedTextColor:Tab被選中字體的顏色;
- app:tabTextColor:Tab未被選中字體的顏色;
- app:tabIndicatorColor:Tab指示器下標(biāo)的顏色;
FloatingActionButton
這是一個浮動按鈕。由于FloatingActionButton是重寫ImageView的,所有FloatingActionButton擁有ImageView的一切屬性。
屬性介紹:
- app:backgroundTint : FAB的背景色。
- app:elevation:FAB的陰影效果。
- app:rippleColor:設(shè)置漣漪的顏色,默認(rèn)是由背景色生成的暗色調(diào),可以自己指定。
- app:pressedTranslationZ:FAB動畫效果,在它被按下的時候陰影就會增大。
CardView
常用屬性:
- app:cardBackgroundColor : 背景顏色
- app:cardCornerRadius : 設(shè)置圓角。
- app:cardElevation : 陰影。
- app:cardMaxElevation : 最大陰影。
- app:cardPreventCornerOverlap : 在v20和之前的版本中添加內(nèi)邊距,這個屬性是為了防止卡片內(nèi)容和邊角的重疊。
- app:cardUseCompatPadding : 設(shè)置內(nèi)邊距,v21+的版本和之前的版本仍舊具有一樣的計算方式
Snackbar
是一種針對操作的輕量級反饋機(jī)制,常以一個小的彈出框的形式,出現(xiàn)在手機(jī)屏幕下方或者桌面左下方。它們出現(xiàn)在屏幕所有層的最上方,包括浮動操作按鈕。
它們會在超時或者用戶在屏幕其他地方觸摸之后自動消失。Snackbar 可以在屏幕上滑動關(guān)閉。當(dāng)它們出現(xiàn)時,不會阻礙用戶在屏幕上的輸入,并且也不支持輸入。屏幕上同時最多只能現(xiàn)實一個 Snackbar。
Android 也提供了一種主要用于提示系統(tǒng)消息的膠囊狀的提示框 Toast。Toast 同 Snackbar 非常相似,但是 Toast 并不包含操作也不能從屏幕上滑動關(guān)閉。
[GitHub很不錯Demo](https://github.com/chrisbanes/cheesesquare)