关于FAB、CardView、Snackbar、TextInputLayout、Palette...

http://www.open-open.com/lib/view/open1416664217867.html

1、着色tint

通过修改图像的Alpha遮罩层来修改图像的颜色,从而达到重新着色的目的,

   <ImageView
   android:src="@mipmap/ic_launcher"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:tint="@color/colorAccent"
   android:tintMode="src_in"/>

效果如下:


tint着色

1、修改视图的形状outline

可以使用ViewOutlineProvider来修改outline,之后再将outline作用于视图

   ViewOutlineProvider viewOutlineProvider=new ViewOutlineProvider() {
       @Override
       public void getOutline(View view, Outline outline) {
           outline.setRoundRect(0,0,outLineImageView.getWidth(),outLineImageView.getHeight(),40);
       }
   };
   outLineImageView.setOutlineProvider(viewOutlineProvider);
   //Sets whether the View's Outline should be used to clip the contents of the View.
   outLineImageView.setClipToOutline(true);
裁剪

2、Palette

Palette可以用来提取颜色,让主题可以适应当前页面的色调
代码如下:

   private void setPalette() {
       Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.ic_1);
       final Palette.Builder build=new Palette.Builder(bitmap);
       build.generate(new Palette.PaletteAsyncListener() {
           @Override
           public void onGenerated(Palette palette) {
               //通过Palette获得对应的色调
               Palette.Swatch swatch=palette.getLightVibrantSwatch();
               getSupportActionBar().setBackgroundDrawable(new ColorDrawable(swatch.getRgb()));
               getWindow().setStatusBarColor(swatch.getRgb());
           }
       });
   }

依赖

compile 'com.android.support:palette-v7:23.3.0'

可以通过如下几个方法获得对应的色调:
getLightVibrantSwatch()、getVibrantSwatch()、getDarkVibrantSwatch()、getLightMutedSwatch()、getMutedSwatch()、getDarkMutedSwatch()


palette

3、FloatingActionButton

FAB用来表示一个APP最主要的操作( promoted action),它主要有以下几个属性:

  • 属性:
    • elevation:正常显示时阴影的大小(FAB在Z轴方向的高度)
    • pressedTranslationZ:按下时阴影的大小(按下时,在Z轴的偏移量)
    • layout_anchor:锚点
  • layout_anchorGrav:相对于锚点的位置
  • backgroundTint:背景色,默认的是theme中的colorAccent
  • rippleColor:按下时的颜色,默认的是theme中的colorControlHighlight
  • fabSize:FAB的大小,normal、mini

代码实现


   <android.support.design.widget.FloatingActionButton
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/ic_tick"
       app:elevation="8dp"
       app:fabSize="normal"
       app:borderWidth="0dp"
       app:pressedTranslationZ="10dp"
       app:backgroundTint="#c96449"
       app:rippleColor="#dfe7ab"
       app:layout_anchor="@id/collapsingToolbarLayout"
       app:layout_anchorGravity="bottom|right"
       app:useCompatPadding="true"/>
FloatingActionButton

注意:
为FAB添加点击事件,才有ripple的效果

  • 样式
  • 在屏幕上只能有一个FAB
  • 图标:默认,56X56dp;最小40X40dp,用来和屏幕中的其他元素创造视觉的连续性
  • icon: 24 x 24dp
Floating action button

参考:
Android Reference FloatingActionButton
Android 设计文档,FAB

4、Snackbar

Snackbar提供了一个可以回调的消息;可以滑出;一个只能同时显示一个Snackbar;高度:48dp (single-line), 80dp (multi-line)

使用的时候,最好将Snackbar与一个CoordinatorLayout关联,这样的话:

  • 可以手动滑出
  • 在Snackbar出现的时候,一些ui元素会被移动
在Snackbar出现的时候,一些ui元素会被移动
Snackbar.make(coordinatorLayout, "floatingActionButton ", Snackbar.LENGTH_LONG).setAction("ok", null).show();

Android training Snackbar
Android Reference Snackbar
Android Snackbar 设计文档

5、CardView

卡片非常适合展示有不同元素组成的内容,比如带有长标题的图片

  • 特点:
  • 有圆角
  • 有多个Action
  • 可以被重新排列、取消
Card的粒子
  • 属性
  • cardElevation:cardView的高度(投影)
  • cardCornerRadius:圆角半径,2dp
  • contentPadding:内边距
  • cardUseCompatPadding:Add padding in API v21+ as well to have the same measurements with previous versions,不明白
  • cardPreventCornerOverlap:为API 20及以前的版本添加padding,防止CardView的内容(ImageView)和CardView的圆角 交叉,效果如下
API 19,cardPreventCornerOverlap为true

但是,有如下限制:
1、使用额外边距
2、不会裁剪其与圆角相交的子视图

API 19,cardPreventCornerOverlap为false
API 23

以上contentPadding="0dp"

代码如下

           <android.support.v7.widget.CardView
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               app:contentPadding="4dp"
               app:cardElevation="2dp"
               app:cardUseCompatPadding="true"
               app:cardCornerRadius="2dp"
               app:cardPreventCornerOverlap="true"
               android:stateListAnimator="@drawable/state_list_animator_selector">
               ...
           </android.support.v7.widget.CardView>

依赖

compile 'com.android.support:cardview-v7:23.0.1'
API 21 以上有效,点击cardView

如果要实现如图点击的效果,添加** stateListAnimator ** 属性,它在API 21以上有效

state_list_animator_selector,代码如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="16dp"
                android:valueType="floatType" />
        </set>
    </item>

    <item android:state_pressed="false">
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="0dp"
                android:valueType="floatType" />
        </set>
    </item>
    
</selector>

注意:为cardView添加点击事件,才有上面这个动画效果

参考:
Android 设计文档,Card
Android Reference cardview

5、TextInputLayout

提供一个显示在EditText上方的浮动标签,效果如下


TextInputLayout ,EditText 校验

代码如下

   <android.support.design.widget.TextInputLayout
       android:layout_margin="30dp"
       android:id="@+id/passwordTextInputLayout"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:counterEnabled="true"
       app:counterMaxLength="10"
       app:counterTextAppearance="@style/MyTextColor"
       app:counterOverflowTextAppearance="@android:color/holo_red_dark">

       <EditText
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:hint="password"
           android:inputType="number"/>

   </android.support.design.widget.TextInputLayout>

</br>

   <style name="MyTextColor" parent="AppTheme">
       <item name="android:textColor">@android:color/holo_red_dark</item>
       <item name="android:textColorHint">@color/colorPrimary</item>
   </style>

</br>

       passwordTextInputLayout.getEditText().addTextChangedListener(new TextWatcher() {
           @Override
           public void beforeTextChanged(CharSequence s, int start, int count, int after) {

           }

           @Override
           public void onTextChanged(CharSequence s, int start, int before, int count) {

           }

           @Override
           public void afterTextChanged(Editable s) {

               if(passwordTextInputLayout.getEditText().getText().toString().length()<6){
                   passwordTextInputLayout.setErrorEnabled(true);
                   passwordTextInputLayout.setError("密码长度小于6位");
               }else {
                   passwordTextInputLayout.setErrorEnabled(false);
               }
           }
       });

注意:TextInputLayout只能包含一个子View

  • TextInputLayout会在左下角生成一个TextView用来显示错误信息这个效果是怎么实现的呢?
    需要借助** setErrorEnabled() **方法,如果传入true,TextInputLayout会在左下角生成一个TextView用来显示错误信息,之后调用setError() 方法,设置错误信息;如果传入false,则移除TextView从而不显示错误信息。

  • 如何统计输入的字数?
    使用 app:counterEnabled="true",app:counterMaxLength="10"

统计输入的字数

</br>

代码 下载
</br>
</br>

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

推荐阅读更多精彩内容