一:介紹
Android5.0之後推出了MaterialDesign(材料設計,物質設計),包括FloatingActionButton(浮動動作按鈕),TextInputLayout(文本輸入佈局,在這個控件下嵌套的EditText的Hint屬性不會隨着用戶輸入消失,而且還有監聽輸入的字符數給出相應提示的功能),TabLayout(表格佈局,類似HorizontalScrollView與viewpager聯動,但是少了很多計算),NavigationView(導航視圖,就是側滑菜單)等等,具體效果可以看下面的效果圖,下面就來介紹這幾種新控件使用方法
二:截圖
1.先來看看FloatingActionButton和TextInputLayout效果圖
2.TabLayout結合viewpager聯動效果圖
3.NavigationView結合DrawerLayout效果圖(就是個側滑菜單效果)
三:實現(IDE:AndroidStudio)
1.首先引入,方式如圖
2.點擊OK之後,先介紹FloatingActionButton和TextInputLayout使用方式
(1) 佈局文件,裏面的字段註釋得很詳細,別忘了把命名空間app引入
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.zidiv.materialdesign.MainActivity">
<!-- app:backgroundTint="#f0f" 背景顏色 -->
<!-- app:elevation="10dp" 周邊陰影寬度 -->
<!-- app:fabSize="normal" 設置大小 -->
<!-- app:rippleColor="#000" 點擊時的顏色 -->
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:src="@mipmap/ic_launcher"
app:backgroundTint="#f0f"
app:elevation="10dp"
app:fabSize="normal"
app:rippleColor="#000" />
<android.support.design.widget.TextInputLayout
android:id="@+id/textinput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入用戶名">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
(2) activity代碼,同樣爲了方便,註釋都在代碼中,裏面有個SnackBar,用法和Toast極其相似,功能和Toast也類似,但是效果不同,可以在上面的第一張效果圖中看到SnackBar的效果,是從底部彈出的提示
package com.zidiv.materialdesign;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
/**
* MaterialDesgin Demo
*/
public class MainActivity extends AppCompatActivity {
private FloatingActionButton fabtn;
private TextInputLayout textinput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
//FloatingActionButton部分,浮動動作按鈕
fabtn = ((FloatingActionButton) findViewById(R.id.fabtn));
fabtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Snackbar snackbar = Snackbar.make(v, "這是一條提醒", Snackbar.LENGTH_SHORT);
snackbar.setAction("我知道了", new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
}
});
snackbar.show();
}
});
//TextInputLayout部分,文本輸入編輯框
textinput = ((TextInputLayout) findViewById(R.id.textinput));
//獲得TextInputLayout包裹的EditText
EditText editText = textinput.getEditText();
//添加一個文本改變監聽器
editText.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) {
//如果輸入的長度超過10
if (s.length() > 10) {
textinput.setError("用戶名長度不能超過10");
textinput.setErrorEnabled(true);
} else {
textinput.setErrorEnabled(false);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
3.然後看看TabLayout的使用方法
(1)佈局文件,字段都註釋在佈局文件中了,同樣別忘了引入命名空間
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.zidiv.materialdesign.TabLayoutActivity">
<!-- app:tabIndicatorColor="#0f0" 下標指示器的顏色 -->
<!-- app:tabIndicatorHeight="1dp" 下標指示器的高度 -->
<!-- app:tabMode="scrollable" 設置表格模式:滾動 -->
<!-- app:tabSelectedTextColor="#f00" 選中的顏色 -->
<!-- app:tabTextColor="#000" 表格文本的顏色 -->
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f"
app:tabIndicatorColor="#0f0"
app:tabIndicatorHeight="1dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="#f00"
app:tabTextColor="#000">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
(2) activity代碼,代碼很少就可以實現
package com.zidiv.materialdesign;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
/**
* 表格佈局和viewpager聯動,效果類似horizontalscrollview和viewpager聯動
*/
public class TabLayoutActivity extends AppCompatActivity {
private TabLayout tablayout;
private ViewPager viewpager;
private List<Fragment> fragmentList = new ArrayList<>();//fragment集合
private List<String> stringList = new ArrayList<>();//字符串集合,用在TabLayout標題上
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout);
initView();
}
private void initView() {
//TabLayout部分,表格佈局
tablayout = ((TabLayout) findViewById(R.id.tablayout));
viewpager = ((ViewPager) findViewById(R.id.viewpager));
//模擬幾個fragment和標題
for (int i = 0; i < 10; i++) {
String title = "標題" + i;
stringList.add(title);
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt("index", i);
fragment.setArguments(bundle);
fragmentList.add(fragment);
}
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList, stringList);
//給viewpager設置適配器
viewpager.setAdapter(adapter);
//設置viewpager和tablayout聯動,很關鍵
tablayout.setupWithViewPager(viewpager);
tablayout.setTabsFromPagerAdapter(adapter);
}
}
(3) activity中MyFragment代碼
package com.zidiv.materialdesign;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by Administrator on 2016/3/3.
*/
public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
int index = getArguments().getInt("index");
textView.setText("fragment" + index);
return textView;
}
}
(4) MyFragmentPageAdapter代碼
package com.zidiv.materialdesign;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
/**
* Created by Administrator on 2016/3/3.
*/
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
private List<String> stringList;
public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> stringList) {
super(fm);
this.fragmentList = fragmentList;
this.stringList = stringList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return stringList.get(position);
}
}
4.最後看看NavigationView的使用
(1) 佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.zidiv.materialdesign.NavigationViewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#f0f"
android:orientation="vertical">
<Button
android:id="@+id/btn_toggle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="開關" />
</LinearLayout>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/activity_main" />
<!-- layout_gravity="left" 從側滑菜單左邊滑出 -->
<!-- app:headerLayout="@layout/headlayout" 加入頭佈局 -->
<!-- app:menu="@menu/main" 加入菜單 -->
<android.support.design.widget.NavigationView
android:id="@+id/navigationview"
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_gravity="left">
<!--app:headerLayout="@layout/headlayout"-->
<!--app:menu="@menu/main">-->
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="哈哈" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
(2) activity代碼
package com.zidiv.materialdesign;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
/**
* 側滑菜單的使用
*/
public class NavigationViewActivity extends AppCompatActivity {
private DrawerLayout drawerlayout;
private NavigationView navigationview;
private boolean flag = false;//側滑菜單是否打開的標識
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_view);
initView();
}
private void initView() {
//側滑的根佈局
drawerlayout = ((DrawerLayout) findViewById(R.id.drawerlayout));
//側滑菜單佈局
navigationview = ((NavigationView) findViewById(R.id.navigationview));
//找到側滑菜單中的控件,可以直接找到
findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("側滑菜單", "點擊了");
Snackbar.make(v, "點擊了", Snackbar.LENGTH_LONG).show();
}
});
//給側滑菜單添加監聽
drawerlayout.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
//當側滑菜單關閉
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
flag = false;
}
//當側滑菜單打開
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
flag = true;
}
});
//側滑開關按鈕
findViewById(R.id.btn_toggle).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag) {//如果是打開的,就關閉側滑菜單
drawerlayout.closeDrawers();
} else {
//佈局中設置從左邊打開,這裏也要設置爲左邊打開
drawerlayout.openDrawer(Gravity.LEFT);
}
}
});
}
}
四:最後的話
到這裏MetarialDesign中的新控件使用就講完了,最後附上demo地址點擊下載demo,三個activity對應上面三個效果圖,你可以在清單文件中配置啓動項,啓動你要啓動的activity就可以了,另外demo是Androidstudio工程,大家導入的時候如果有錯誤請修改自己的build.gradle文件即可,謝謝大家!