Android 新控件學(xué)習(xí)

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,505評論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,556評論 3 418
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,463評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,009評論 1 312
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,778評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,218評論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,281評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,436評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,969評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,795評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,993評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,537評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,229評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,659評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,917評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,687評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,990評論 2 374

推薦閱讀更多精彩內(nèi)容

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