Android 新控件學習

Android新控件

  • DrawerLayout 頂層容器,通常與NavigationView實現側滑菜單
  • NavigationView 導航菜單
  • CoordinatorLayout 一個超級強大的FrameLayout,主要用于根布局、與子視圖做特定交互
  • AppBarLayout MD風格滑動布局,子視圖需提供滾動行為
  • CollapsingToolbarLayout 對ToolBar再次包裝
  • ToolBar 工具欄、導航欄
  • TabLayout 官方tabs組件,通常與ViewPager組合實現主內容區域
  • ViewPager 可以左右滑動的布局管理器
  • FloatingActionButton 浮動按鈕
  • TextInputLayout 包裹EditText,實現帶提示EditText
  • SnackBar 類似Toast輕量級提示控件
  • CardView 卡片式View,輕松實現圓角陰影效果

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>

通常主要布局內容布局放在DrawerLayout中第一個位置,第二個位置放DrawerLayout中的內容,通常與NavigationView結合實現抽屜式側滑菜單也可使用其他布局;
打開側滑菜單
drawerLayout.openDrawer(GravityCompat.START)
關閉側滑菜單
drawerLayout.closeDrawers()

NavigationView

app:headerLayout:NavigationView中頭部的head部分的布局,自己定義一個布局;
app:menu:指定NavigationView中的Menu布局,就是自己寫Menu中的按鈕,要放在res/menu/文件夾下;

CoordinatorLayout

一個增強型的FrameLayout,可以實現與子布局交互,比如滾動的工具欄(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設置為Theme.AppCompat.Light.NoActionBar的方式

AppBarLayout

繼承LinearLayout,方向垂直,當外部某個可滾動控件滾動時,可定制內部控件如何移動;
通過設置子View :app:layout_scrollFlags屬性來控制子View執行動作:

  • scroll: 值設為scroll的View會跟隨滾動事件一起發生移動。就是當指定的ScrollView發生滾動時,該View也跟隨一起滾動,就好像這個View也是屬于這個ScrollView一樣。
  • enterAlways:值設為enterAlways的View,當ScrollView往下滾動時,該View會直接往下滾動。而不用考慮ScrollView是否在滾動。
  • exitUntilCollapsed:值設為exitUntilCollapsed的View,當這個View要往上逐漸“消逝”時,會一直往上滑動,直到剩下的的高度達到它的最小高度后,再響應ScrollView的內部滑動事件。
  • exitUntilCollapsed:值設為exitUntilCollapsed的View,當這個View要往上逐漸“消逝”時,會一直往上滑動,直到剩下的的高度達到它的最小高度后,再響應ScrollView的內部滑動事件。
    需要注意的是,后面兩種模式基本只有在CollapsingToolbarLayout才有用;
    將AppBarLayout與滑動控件關聯起來需指定Behavior,即屬性:app:layout_behavior="@string/appbar_scrolling_view_behavior"

CollapsingToolbarLayout

CollapsingToolbarLayout是用來對Toolbar進行再次包裝的ViewGroup,主要是用于實現折疊(其實就是看起來像伸縮~)的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搭配使用可以增大界面的內容展示量,實現各種個性化分類內容展示而不互相干擾!
常用屬性:
有以下常用屬性:

  • app:tabGravity="fill" 表示TabLayout中的Tabs要占滿屏幕的width;
  • app:tabSelectedTextColor:Tab被選中字體的顏色;
  • app:tabTextColor:Tab未被選中字體的顏色;
  • app:tabIndicatorColor:Tab指示器下標的顏色;

FloatingActionButton

這是一個浮動按鈕。由于FloatingActionButton是重寫ImageView的,所有FloatingActionButton擁有ImageView的一切屬性。
屬性介紹:

  • app:backgroundTint : FAB的背景色。
  • app:elevation:FAB的陰影效果。
  • app:rippleColor:設置漣漪的顏色,默認是由背景色生成的暗色調,可以自己指定。
  • app:pressedTranslationZ:FAB動畫效果,在它被按下的時候陰影就會增大。

CardView

常用屬性:

  • app:cardBackgroundColor : 背景顏色
  • app:cardCornerRadius : 設置圓角。
  • app:cardElevation : 陰影。
  • app:cardMaxElevation : 最大陰影。
  • app:cardPreventCornerOverlap : 在v20和之前的版本中添加內邊距,這個屬性是為了防止卡片內容和邊角的重疊。
  • app:cardUseCompatPadding : 設置內邊距,v21+的版本和之前的版本仍舊具有一樣的計算方式

Snackbar

是一種針對操作的輕量級反饋機制,常以一個小的彈出框的形式,出現在手機屏幕下方或者桌面左下方。它們出現在屏幕所有層的最上方,包括浮動操作按鈕。

它們會在超時或者用戶在屏幕其他地方觸摸之后自動消失。Snackbar 可以在屏幕上滑動關閉。當它們出現時,不會阻礙用戶在屏幕上的輸入,并且也不支持輸入。屏幕上同時最多只能現實一個 Snackbar。

Android 也提供了一種主要用于提示系統消息的膠囊狀的提示框 Toast。Toast 同 Snackbar 非常相似,但是 Toast 并不包含操作也不能從屏幕上滑動關閉。

[GitHub很不錯Demo](https://github.com/chrisbanes/cheesesquare

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,523評論 25 708
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,569評論 2 45
  • 一個玩具廠商做了20年還不足為驚奇,但對于一個游戲開發商而言,歷經二十年還能在市場上擁有眾多粉絲的青睞實屬不易...
    Doreen乖乖美美噠閱讀 308評論 3 1
  • 那天,為了改善高三枯燥的伙食,我一路小跑來到麥當勞,拿著一紙袋好吃的站在海淀黃莊的路口等紅燈,準備飛奔回學校用好每...
    LEA_Zu閱讀 228評論 1 2
  • 原諒我寫出這么尷尬到臉酸的文字。 不得不說,每隔一段時間,總會有這樣的時刻:做任何事情都抬不起興趣,心情也不是郁悶...
    jennydeer閱讀 196評論 0 0