material design 筆記2
一.沉浸
1.什么是沉浸鸠天?傳統(tǒng)的手機(jī)狀態(tài)欄是呈現(xiàn)出黑色條狀的讼育,有的和手機(jī)主界面有很明顯的區(qū)別。這一樣就在一定程度上犧牲了視覺寬度稠集,界面面積變小奶段。沉浸式是APP界面圖片延伸到狀態(tài)欄, 應(yīng)用本身沉浸于狀態(tài)欄剥纷,所以如果第三方的軟件沒有為狀態(tài)欄分配圖片痹籍,那么自然就是黑色。頂端的狀態(tài)欄和下面的虛擬按鍵都隱藏晦鞋,需要的時(shí)候從邊緣劃出蹲缠。
當(dāng)啟用該模式棺克,應(yīng)用程序的界面將占據(jù)整個(gè)屏幕,系統(tǒng)自動(dòng)將隱藏系統(tǒng)的狀態(tài)欄和導(dǎo)航欄线定,讓應(yīng)用程序內(nèi)容可以在最大顯示范圍呈現(xiàn)娜谊,增加大屏體驗(yàn),而當(dāng)需要查看通知的時(shí)候只需要從頂部向下滑動(dòng)就能呼出通知欄斤讥。
沉浸模式實(shí)際上有兩種:
一種叫“沉浸模式”纱皆,狀態(tài)欄和虛擬按鈕會(huì)自動(dòng)隱藏、應(yīng)用自動(dòng)全屏芭商,這種模式下派草,應(yīng)用占據(jù)屏幕的全部空間, 只有當(dāng)用戶從屏幕的上方邊沿處向下劃動(dòng)時(shí)铛楣, 才會(huì)退出沉浸模式近迁, 用戶觸摸屏幕其它部分時(shí), 不會(huì)退出該模式蛉艾, 這種模式比較適用于閱讀器钳踊、 雜志類應(yīng)用。
另外一種叫“黏性沉浸模式”勿侯,讓狀態(tài)欄和虛擬按鈕半透明,應(yīng)用使用屏幕的全部空間缴罗, 當(dāng)用戶從屏幕的上方邊沿處向下滑動(dòng)時(shí)助琐,也不會(huì)退出該模式, 但是系統(tǒng)界面 (狀態(tài)欄面氓、 導(dǎo)航欄) 將會(huì)以半透明的效果浮現(xiàn)在應(yīng)用視圖之上 兵钮, 只有當(dāng)用戶點(diǎn)擊系統(tǒng)界面上的控件時(shí), 才會(huì)退出黏性沉浸模式舌界。
2.Android 實(shí)現(xiàn)沉浸
android從4.4開始掘譬,開始支持UI使用StatusBar與NavigationBar的范圍。
所以要進(jìn)行下面的配置:
在value中的styles.xml中設(shè)置
<!-- Base application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme" parent="AppTheme.Base"></style>
在value-v19中的styles.xml中設(shè)置(為了兼容4.4)
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
在value-v21中的styles.xml中設(shè)置
<style name="AppTheme" parent="AppTheme.Base">
<!--透明狀態(tài)欄-->
<item name="android:windowTranslucentStatus">true</item>
<!--透明導(dǎo)航欄-->
<item name="android:windowTranslucentNavigation">true</item>
<!--使?fàn)顟B(tài)欄呻拌,導(dǎo)航欄可繪制-->
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
</style>
二.布局控件
1.CardView葱轩、RecyclerView
Android L(5.0)中新增了兩個(gè)控件分別是CardView,RecyclerView藐握,RecyclerView這個(gè)控件是一個(gè)可以裝載大量的視圖集合靴拱,并且可以非常效率的進(jìn)行回收和滾動(dòng)。當(dāng)你list中的元素經(jīng)常動(dòng)態(tài)改變時(shí)可以使用RecyclerView控件猾普。
RecyclerView非常容易使用袜炕,它提供了如下兩個(gè)功能:
為每個(gè)條目位置提供了layout管理器(RecyclerView.setLayoutManager)
為每個(gè)條目設(shè)置了操作動(dòng)畫(RecyclerView.setItemAnimator)
CardView
CardView繼承自FrameLayout,允許你在card視圖中顯示信息. CardView也可以設(shè)置陰影和圓角初家。
CardView在API 21以下的圓角效果處理
2.com.android.support:design
a.TextInputLayout 提示輸入內(nèi)容
登錄時(shí)可以提示偎窘。
比如:
<android.support.design.widget.TextInputLayout
android:id="@+id/input_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterTextAppearance="@style/counterTextAppearance"
app:errorTextAppearance="@style/errorTextAppearance"
app:hintTextAppearance="@style/hintTextAppearance">
<AutoCompleteTextView
android:id="@+id/tv_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/login_user_name_hint"
android:imeOptions="actionNext"
android:maxLength="20"
android:maxLines="1"
android:singleLine="true"
android:textSize="@dimen/NormalTextSize" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterTextAppearance="@style/counterTextAppearance"
app:errorTextAppearance="@style/errorTextAppearance"
app:hintTextAppearance="@style/hintTextAppearance"
app:passwordToggleEnabled="true">
<EditText
android:id="@+id/tv_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/login_user_name_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:maxLength="20"
android:maxLines="1"
android:singleLine="true"
android:textSize="@dimen/NormalTextSize" />
</android.support.design.widget.TextInputLayout>
注意:
app:counterTextAppearance="@style/counterTextAppearance"
app:errorTextAppearance="@style/errorTextAppearance"
app:hintTextAppearance="@style/hintTextAppearance">
可以自己在style中定義 暗示文字乌助、提示文字、錯(cuò)誤文字陌知、以及的顏色 大小等風(fēng)格眷茁。
<!-- 登錄相關(guān)-->
<style name="errorTextAppearance" parent="@style/TextAppearance.Design.Error">
<item name="android:textSize">18sp</item>
</style>
<style name="counterTextAppearance" parent="@style/TextAppearance.Design.Counter">
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:textSize">20sp</item>
</style>
<style name="hintTextAppearance" parent="@style/TextAppearance.Design.Hint">
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:textSize">20sp</item>
</style>
b.TabLayout
一般與ViewPager配合 fragment一起使用
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/tabLayout_hight"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/black_overlay"
app:tabIndicatorHeight="@dimen/tabIndicatorHeight"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/black"
app:tabTextAppearance="@style/TabLayoutTextStyle"
app:tabTextColor="@color/tablayout_select" />
<ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
其中 app:tabMode 兩種模式 scrollable 可滾動(dòng)的; 卷動(dòng); 可卷動(dòng)的;fixed 固定的,不變的;
app:tabGravity 兩種 center 聚集在中間纵诞, fill充滿整個(gè)bar平均分上祈。
TabLayout在布局底部是自帶一條滾動(dòng)條的,為了實(shí)現(xiàn)塊狀切換的效果浙芙,可以把 app:tabIndicatorHeight 屬性設(shè)置為0dp登刺,去掉滾動(dòng)條
app:tabIndicatorHeight="0dp"
每個(gè)tab添加分割線
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mTabLayout = (TabLayout) findViewById(R.id.tabLayout);
LinearLayout linearLayout = (LinearLayout) mTabLayout.getChildAt(0);
linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,
R.drawable.layout_divider_vertical));
在drawable 下邊定義layout_divider_vertical
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/black"/>
<size android:width="1dp"/>
</shape>
更改TabLayout選中的背景色
在drawable文件夾下建立選中效果的文件 tab_background_selected.xml和沒選中的效果文件tab_background_unselected.xml color不同而已。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorPrimaryDark"/>
</shape>
在drawable文件夾下建立文件 tab_background_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_background_selected" android:state_selected="true" />
<item android:drawable="@drawable/tab_background_unselected" android:state_focused="false" android:state_pressed="false" android:state_selected="false" />
</selector>
最后在Style 中新建 Base.Widget.Design.TabLayout 復(fù)寫
<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
<item name="tabBackground">@drawable/tab_selector</item>
</style>
繼續(xù)整理 2017年10月17日 19:22:56
AppBarLayout
用AppBarLayout可以讓包含在其中的子控件能響應(yīng)被標(biāo)記了ScrollingViewBehavior的的滾動(dòng)事件(要與CoordinatorLayout 一起使用)嗡呼,利用它我們可以很容易的去實(shí)現(xiàn)視差滾動(dòng)效果纸俭。
CoordinatorLayout
協(xié)調(diào)(Coordinate)其他組件, 實(shí)現(xiàn)聯(lián)動(dòng).
掌握 Coordinator Layout
AppbarLayout 嚴(yán)重依賴于CoordinatorLayout,必須用于CoordinatorLayout 的直接子View南窗。
layout_scrollFlags有5種動(dòng)作揍很,分別是 scroll,enterAlways,enterAlwaysCollapsed,exitUntilCollapsed,snap。
- scroll ,子View 添加layout_scrollFlags屬性 的值scroll 時(shí)万伤,這個(gè)View將會(huì)隨著可滾動(dòng)View(如:ScrollView,以下都會(huì)用ScrollView 來代替可滾動(dòng)的View )一起滾動(dòng)窒悔,就好像子View 是屬于ScrollView的一部分一樣。
2.enterAlways ,子View 添加layout_scrollFlags屬性 的值有enterAlways 時(shí), 當(dāng)ScrollView 向下滑動(dòng)時(shí)敌买,子View 將直接向下滑動(dòng)简珠,而不管ScrollView 是否在滑動(dòng)。注意:要與scroll 搭配使用虹钮,否者是不能滑動(dòng)的聋庵。
- enterAlwaysCollapsed , enterAlwaysCollapsed 是對(duì)enterAlways 的補(bǔ)充,當(dāng)ScrollView 向下滑動(dòng)的時(shí)候芙粱,滑動(dòng)View(也就是設(shè)置了enterAlwaysCollapsed 的View)下滑至折疊的高度祭玉,當(dāng)ScrollView 到達(dá)滑動(dòng)范圍的結(jié)束值的時(shí)候,滑動(dòng)View剩下的部分開始滑動(dòng)春畔。這個(gè)折疊的高度是通過View的minimum height (最小高度)指定的脱货。要配合scroll|enterAlways 一起使用.
4.exitUntilCollapsed, 當(dāng)ScrollView 滑出屏幕時(shí)(也就時(shí)向上滑動(dòng)時(shí)),滑動(dòng)View先響應(yīng)滑動(dòng)事件拐迁,滑動(dòng)至折疊高度蹭劈,也就是通過minimum height 設(shè)置的最小高度后,就固定不動(dòng)了线召,再把滑動(dòng)事件交給 scrollview 繼續(xù)滑動(dòng)铺韧。
5.snap,意思是:在滾動(dòng)結(jié)束后,如果view只是部分可見缓淹,它將滑動(dòng)到最近的邊界哈打。比如塔逃,如果view的底部只有25%可見料仗,它將滾動(dòng)離開屏幕湾盗,而如果底部有75%可見,它將滾動(dòng)到完全顯示氛改。
CollapsingToolbarLayout帐萎、
讓Toolbar可伸縮,在伸縮的時(shí)候決定ToolBar是移除屏幕和固定在最上面胜卤。
屬性意義
1)contentScrim:當(dāng)Toolbar收縮到一定程度時(shí)的所展現(xiàn)的主體顏色疆导。即Toolbar的顏色。
2)title:當(dāng)titleEnable設(shè)置為true的時(shí)候葛躏,在toolbar展開的時(shí)候澈段,顯示大標(biāo)題,toolbar收縮時(shí)舰攒,顯示為toolbar上面的小標(biāo)題败富。
3)scrimAnimationDuration:該屬性控制toolbar收縮時(shí),顏色變化的動(dòng)畫持續(xù)時(shí)間芒率。即顏色變?yōu)閏ontentScrim所指定的顏色進(jìn)行的動(dòng)畫所需要的時(shí)間囤耳。
4)expandedTitleGravity:指定toolbar展開時(shí),title所在的位置偶芍。類似的還有expandedTitleMargin、collapsedTitleGravity這些屬性德玫。
5)collapsedTitleTextAppearance:指定toolbar收縮時(shí)匪蟀,標(biāo)題字體的樣式,
層級(jí):
<android.support.design.widget.CoordinatorLayout...>
<android.support.design.widget.AppBarLayout...>
<android.support.design.widget.CollapsingToolbarLayout...>
<!-- your collapsed view -->
<View.../>
<android.support.v7.widget.Toolbar.../>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!-- Scroll view -->
<android.support.v7.widget.RecyclerView.../>
</android.support.design.widget.CoordinatorLayout>
FloatingActionButton
浮動(dòng)按鈕
app:borderWidth=""------------------邊框?qū)挾仍咨ǔTO(shè)置為0 材彪,用于解決Android 5.X設(shè)備上陰影無法正常顯示的問題
app:backgroundTint=""---------------按鈕的背景顏色,不設(shè)置琴儿,默認(rèn)使用theme中colorAccent的顏色
app:rippleColor=""--------------------點(diǎn)擊的邊緣陰影顏色
app:elevation=""----------------------邊緣陰影的寬度
app:pressedTranslationZ="16dp"-----點(diǎn)擊按鈕時(shí)段化,按鈕邊緣陰影的寬度,通常設(shè)置比elevation的數(shù)值大
NavigationView造成、
抽屜式的導(dǎo)航控件
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.mobiletrain.drawerlayout.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主頁面"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header_layout"
app:menu="@menu/main"></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
1.android:layout_gravity="left"屬性表示該View是左邊的滑出菜單显熏,這個(gè)屬性的含義不用多說,這是DrawerLayout使用方式中的知識(shí)點(diǎn)晒屎。
2.app:headerLayout="@layout/header_layout"表示引用一個(gè)頭布局文件喘蟆,這個(gè)頭就是我們?cè)谏厦婵吹降哪莻€(gè)背景圖片缓升,包括背景圖片上面的顯示用戶名的控件等等。
3.app:menu="@menu/main"表示引用一個(gè)menu作為下面的點(diǎn)擊項(xiàng)
Snackbar
SnackBar通過在屏幕底部展示簡潔的信息蕴轨,為一個(gè)操作提供了一個(gè)輕量級(jí)的反饋港谊,并且在Snackbar中還可以包含一個(gè)操作,在同一時(shí)間內(nèi)橙弱,僅且只能顯示一個(gè) Snackbar歧寺,它的顯示依賴于UI,不像Toast那樣可以脫離應(yīng)用顯示棘脐。它的用法和Toast很相似斜筐,唯一不同的就是它的第一個(gè)參數(shù)不是傳入Context而是傳入它所依附的父視圖,但是他比Toast更強(qiáng)大荆残。
未實(shí)際應(yīng)用奴艾,參考
參考http://www.reibang.com/p/cd1e80e64311/