Material Design入门(二)
本文主要包括以下內容
DrawerLayout介紹
drawerLayout是Support Library包中實現了側滑菜單效果的控件,可以說drawerLayout是因為第三方控件如MenuDrawer等的出現之后,google借鑒而出現的產物。drawerLayout分為側邊菜單和主內容區兩部分,側邊菜單可以根據手勢展開與隱藏(drawerLayout自身特性),主內容區的內容可以隨著菜單的點擊而變化(這需要使用者自己實現)
drawlayout實現
main布局文件
<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:fitsSystemWindows="true"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="?attr/colorPrimary"app:layout_collapseMode="pin"app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /></LinearLayout><android.support.design.widget.NavigationViewandroid:id="@+id/navigation_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="start"app:headerLayout="@layout/navigation_header"app:menu="@menu/drawer" /></android.support.v4.widget.DrawerLayout>其中側滑菜單位置是start,并且包括了headerLayout與menu
headerLayout實現
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/lib/com.zj.material3navigation"android:layout_width="match_parent"android:layout_height="192dp"android:background="?attr/colorPrimaryDark"android:gravity="center"android:orientation="vertical"android:padding="16dp"android:theme="@style/ThemeOverlay.AppCompat.Dark"><de.hdodenhof.circleimageview.CircleImageView android:id="@+id/profile_image"android:layout_width="72dp"android:layout_height="72dp"android:layout_marginTop="20dp"android:src="@mipmap/profile"app:border_color="@color/primary_light"app:border_width="2dp" /><TextView android:layout_marginTop="10dp"android:textSize="18sp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="APP開發者"android:gravity="center"android:textAppearance="@style/TextAppearance.AppCompat.Body1"/></LinearLayout>注意,由于使用了CircleImageView,要在depencyies中加入
compile 'de.hdodenhof:circleimageview:1.3.0'并且由于jcenter中庫有限,可能還要加入
allprojects {repositories {jcenter()maven { url "https://jitpack.io" }} }menu菜單實現
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"><group android:checkableBehavior="single"><item android:id="@+id/navigation_item_example"android:icon="@drawable/ic_favorite"android:title="@string/navigation_example" /><item android:id="@+id/navigation_item_blog"android:icon="@drawable/ic_favorite"android:title="@string/navigation_my_blog" /><item android:id="@+id/navigation_item_about"android:icon="@drawable/ic_favorite"android:title="@string/navigation_about" /></group></menu>menu也可以設置子menu,下面是一個例子
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"><group android:checkableBehavior="single"><item android:id="@+id/drawer_home"android:checked="true"android:icon="@drawable/ic_home_black_24dp"android:title="@string/home"/><item android:id="@+id/section"android:icon="@drawable/ic_more_horiz_black_24dp"android:title="分組1"><menu><item android:id="@+id/drawer_favourite"android:icon="@drawable/ic_favorite_black_24dp"android:title="@string/favourite"/><item android:id="@+id/drawer_downloaded"android:icon="@drawable/ic_file_download_black_24dp"android:title="@string/downloaded"/></menu></item><item android:id="@+id/section2"android:title="分組2"><menu><item android:id="@+id/drawer_more"android:icon="@drawable/ic_more_horiz_black_24dp"android:title="@string/more"/><item android:id="@+id/drawer_settings"android:icon="@drawable/ic_settings_black_24dp"android:title="@string/settings"/></menu></item></group> </menu>效果如下
java代碼的實現
package com.zj.material3navigation;import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View;public class MainActivity extends AppCompatActivity {Toolbar mToolbar;DrawerLayout mDrawerLayout;ActionBarDrawerToggle mDrawerToggle;NavigationView mNavigationView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//設置ToolbarmToolbar = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(mToolbar);setTitle("startNow");//設置DrawerLayoutmDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,R.string.drawer_open, R.string.drawer_close){//關閉側邊欄時響應@Overridepublic void onDrawerClosed(View drawerView) {super.onDrawerClosed(drawerView);}//打開側邊欄時響應@Overridepublic void onDrawerOpened(View drawerView) {super.onDrawerOpened(drawerView);}};mDrawerToggle.syncState();mDrawerLayout.setDrawerListener(mDrawerToggle);//設置NavigationView點擊事件mNavigationView = (NavigationView) findViewById(R.id.navigation_view);setupDrawerContent(mNavigationView);//設置NavigationView點擊事件}//點擊側邊欄菜單的響應事件private void setupDrawerContent(NavigationView navigationView) {navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {@Overridepublic boolean onNavigationItemSelected(MenuItem menuItem) {switch (menuItem.getItemId()) {case R.id.navigation_item_example://switchToExample();break;case R.id.navigation_item_blog://switchToBlog();break;case R.id.navigation_item_about://switchToAbout();break;}menuItem.setChecked(true);mDrawerLayout.closeDrawers();return true;}});}}實現了打開與關閉側邊欄的響應事件,點擊側邊欄按鈕的響應事件等
參考鏈接:
Design Support Library (I): Navigation View的使用 - 泡在網上的日子
android官方側滑菜單DrawerLayout詳解 - 泡在網上的日子
效果如下:
CardView實現
首先加入依賴庫
dependencies {....compile 'com.android.support:cardview-v7:22.2.0' }layout布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><android.support.v7.widget.Toolbarandroid:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:background="?attr/colorPrimary"></android.support.v7.widget.Toolbar><android.support.v4.widget.NestedScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:scrollbars="none"xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingTop="5dp"xmlns:android="http://schemas.android.com/apk/res/android"> <android.support.v7.widget.CardViewandroid:layout_width="match_parent"android:layout_height="wrap_content"app:cardCornerRadius="10dp"app:cardElevation="10dp"android:layout_marginBottom="@dimen/card_margin"android:layout_marginLeft="@dimen/card_margin"android:layout_marginRight="@dimen/card_margin"xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:clickable="true"android:foreground="?android:attr/selectableItemBackground"><LinearLayoutstyle="@style/CardView.Content"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/book1" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/book_title_1"android:textAppearance="@style/TextAppearance.AppCompat.Title"android:textColor="@color/primary_text" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:text="@string/book_description_1"android:textColor="@color/secondary_text" /></LinearLayout></LinearLayout></android.support.v7.widget.CardView><android.support.v7.widget.CardViewxmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="@dimen/card_margin"android:layout_marginLeft="@dimen/card_margin"android:layout_marginRight="@dimen/card_margin"android:layout_marginTop="@dimen/card_margin"android:onClick="goDetail"app:cardCornerRadius="10dp"app:cardElevation="10dp"><LinearLayoutstyle="@style/CardView.Content"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/book2" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/book_title_2"android:textAppearance="@style/TextAppearance.AppCompat.Title"android:textColor="@color/primary_text" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:text="@string/book_description_2"android:textColor="@color/secondary_text" /></LinearLayout></LinearLayout></android.support.v7.widget.CardView><android.support.v7.widget.CardViewxmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="@dimen/card_margin"android:layout_marginLeft="@dimen/card_margin"android:layout_marginRight="@dimen/card_margin"android:layout_marginTop="@dimen/card_margin"android:onClick="goDetail"app:cardCornerRadius="10dp"app:cardElevation="10dp"><LinearLayoutstyle="@style/CardView.Content"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/book3" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/book_title_3"android:textAppearance="@style/TextAppearance.AppCompat.Title"android:textColor="@color/primary_text" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="2dp"android:text="@string/book_description_3"android:textColor="@color/secondary_text" /></LinearLayout></LinearLayout></android.support.v7.widget.CardView> </LinearLayout></android.support.v4.widget.NestedScrollView></LinearLayout>- app:cardBackgroundColor 設置CardView背景顏色
- app:cardCornerRadius 設置CardView圓角大小
- app:cardElevation 設置CardView陰影高度
添加波紋點擊效果
默認情況,CardView是不可點擊的,并且沒有任何的觸摸反饋效果。觸摸反饋動畫在用戶點擊CardView時可以給用戶以視覺上的反饋。為了實現這種行為,你必須提供一下屬性:
<android.support.v7.widget.CardView...android:clickable="true"android:foreground="?android:attr/selectableItemBackground">... </android.support.v7.widget.CardView>在加載圖片時可能會遇到圖上尺寸的問題
(1)drawable-hdpi里面存放高分辨率的圖片,如WVGA (480x800),FWVGA (480x854)
(2)drawable-mdpi里面存放中等分辨率的圖片,如HVGA (320x480)
(3)drawable-ldpi里面存放低分辨率的圖片,如QVGA (240x320)
ldpi:240x320
mdpi:320x480
hdpi:480x800、480x854
xhdpi:至少960*720
xxhdpi:1280×720
從上表可以得出如下結論
圖片放在drawable中,等同于放在drawable-mdpi中,原因為:drawable目錄不具有屏幕密度特性,所以采用基準值,即mdpi
圖片放在某個特定drawable中,比如drawable-hdpi,如果設備的屏幕密度高于當前drawable目錄所代表的密度,則圖片會被放大,否則會被縮小
放大或縮小比例 = 設備屏幕密度 / drawable目錄所代表的屏幕密度
為了更全面的適配所有設備,我們應該提供一套針對主流屏幕密度的圖片(目前為hdpi或xhdpi),其他密度通過系統自動縮放得到圖片
參考鏈接:
Android開發–CardView使用-愛編程
Android中屏幕密度和圖片大小的關系分析 - Android移動開發技術文章_手機開發 - 紅黑聯盟
res里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi) - xfyn - 博客園
效果如下
總結
以上是生活随笔為你收集整理的Material Design入门(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Smarty模板技术学习
- 下一篇: LeNet网络配置文件 lenet_tr