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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,050评论 25 707
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,360评论 2 44
  • 一个玩具厂商做了20年还不足为惊奇,但对于一个游戏开发商而言,历经二十年还能在市场上拥有众多粉丝的青睐实属不易...
    Doreen乖乖美美哒阅读 299评论 3 1
  • 那天,为了改善高三枯燥的伙食,我一路小跑来到麦当劳,拿着一纸袋好吃的站在海淀黄庄的路口等红灯,准备飞奔回学校用好每...
    LEA_Zu阅读 213评论 1 2
  • 原谅我写出这么尴尬到脸酸的文字。 不得不说,每隔一段时间,总会有这样的时刻:做任何事情都抬不起兴趣,心情也不是郁闷...
    jennydeer阅读 182评论 0 0