Android中MD相關的控件介紹及使用

Material Design 中有八個控件分別是:

首先,在 gradle 文件中引入 meterial design 庫:

compile 'com.android.support:design:22.2.0'


1.CoordinatorLayout其實就是類似於一個FrameLayout佈局,該佈局的強大之處在於能夠協調各個view之間的關係,只需要將各個view包含Coordinator中即可,


2.AppBarLayout 它繼承於LinerLayout,依賴於CoordinatorLayout,必須是它下面的子view,否則放到其他ViewGroup下面是無效的。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

3.CollapsingToolbarLayout 主要是用來對Toolbar進行包裝的ViewGroup,主要用於實現摺疊的Appbar的效果,它需要放在AppBarLayout的佈局裏,作爲它的直接子View。

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >
    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:scaleType="centerCrop"
            android:src="@drawable/material"
            app:layout_collapseMode="parallax"
            />
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:layout_collapseMode="pin"
            />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
4.NavigationView是在做抽屜佈局的時候左滑出來的部分,原來都是自定義,現在Google在5.0之後推出來這樣一這樣簡單而又方便的組件,這個菜單大概分爲兩部分,上面一部分叫HeaderLayout,下面一部分點擊項叫menu,首先把要把它放到一個DrawerLayout中,然後可以直接使用NavigationView,其中headLayout就是上面的一個頭佈局文件可以 設置一下高度,一個背景看一個效果圖,ment就是點擊項。

<?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>

5.NestedScrollView和ScrollView基本功能是一致的,只不過NetstedScrollView可以兼容新的控件,關鍵在於和ToolBar交互產生的作用。

app:layout_behavior="@string/appbar_scrolling_view_behavior"和
CollapsingToolbarLayout中的
app:layout_scrollFlags="scroll|exitUntilCollapsed"是對應的,
一旦發現NestedScrollView中有那句話,就會檢查其他的控件,然後如果有上面這句話對應的話,滑動的效果就可以實現的。

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="50dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/my_txt"
        android:textSize="20sp" />

</android.support.v4.widget.NestedScrollView>

6.FloatingActionButton 就一個漂浮的按鈕,這個類是繼承於ImageView的,所以對於它可以使用ImageView的所有屬性。

<android.support.design.widget.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/floatbutton"
android:src="@mipmap/ok"
app:borderWidth="0dp"
app:rippleColor="#33728dff"
app:elevation="8dp"
app:pressedTranslationZ="16dp"
        />

7.TabLayout類似於今日頭條上的每一個tab,一般是和ViewPager聯動,可滑動。
<android.support.design.widget.TabLayout
    android:id="@+id/tablayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="scrollable"
    app:tabIndicatorColor="@color/ssxinhongse1"
    app:tabTextAppearance="@style/TextAppearanceBig"
    app:tabSelectedTextColor="@color/ssxinhongse1"
    />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/ssxinheise2_press"
    />
<android.support.v4.view.ViewPager
    android:id="@+id/vp"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>

8.SnackBar類似於Toast的一個控件。

 Snackbar snackbar =
            Snackbar.make(fab, "過年了,過年了", Snackbar.LENGTH_LONG)
                    .setAction("去過年", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(MainActivity.this, "你點擊了右邊的按鈕", Toast.LENGTH_LONG).show();                    }
                    });
    //獲取Snackbar的view
    View snackbarview = snackbar.getView();
    Snackbar.SnackbarLayout snackbarLayout(Snackbar.SnackbarLayout)snackbarview;
	ckbarview.setBackgroundColor(0xffffffff);
    //要添加圖片的佈局
    View add_view = LayoutInflater.from(snackbarview.getContext()).inflate(R.layout.snackbar_main,null);
	//文字的顏色
	((TextView) snackbarview.findViewById(R.id.snackbar_text)).setTextColor(Color.parseColor("#FF0000"));
	//按鈕的顏色
	((Button)snackbarview.findViewById(R.id.snackbar_action)).setTextColor(Color.parseColor("#ff0000"));
    	LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    	p.gravity= Gravity.CENTER_VERTICAL;
	//添加布局
	snackbarLayout.addView(add_view, 0, p);
	snackbar.show();
以上是我簡單的總結的,希望對大家會有所幫助,更好的使用,有問題隨時扣我,謝謝大家。
QQ:1119348287(微信同步)。

發佈了43 篇原創文章 · 獲贊 14 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章