Android进阶系列--Design Support Library使用详解(Snackbar,TextInputLayout,TabLayout,NavigationView...)
Posted xinruzhishui_11
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android进阶系列--Design Support Library使用详解(Snackbar,TextInputLayout,TabLayout,NavigationView...)相关的知识,希望对你有一定的参考价值。
原文链接:http://blog.csdn.net/sw5131899/article/details/53996800
*本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布
Material Design 设计风格非常受欢迎,那么支持其效果的Design Support Library(Android 2.1 API level 7及其以上)库又有哪些控件呢。主要包括SnackBar、Navigation View、FloatActionbutton、CoordinatorLayout、CollapsingToolBarLayout等。我在Git上看见一个非常炫的效果
谷歌官网介绍:http://android-developers.blogspot.com.es/2012/05/android-design-support-library.html
把该项目的Git附上,觉得有用的自行下载看源码:https://github.com/frogermcs/InstaMaterial,现在来一一介绍Design系列控件。这里还有极客学院整理的关于Material Design的文档:
http://wiki.jikexueyuan.com/project/material-design/components/snackbars-and-toasts.html
1.SnackBar
SnackBar是带有动画效果的快速提示栏,它显示在屏幕底部,是用来代替Toast的一个全新控件,它基本上继承了Toast的属性和方法,用户可以点击按钮执行对应的操作,Snackbar支持滑动消失,如果没设任何操作,那么到时间自动消失。
SnackBar的构造:
// 参数分别是父容器,提示信息,持续时间
public static Snackbar make(@NonNull View view, @NonNull CharSequence text,@Duration int duration)
SnackBar的常用方法:
// 用于给SnackBar设定一个Action,点击之后会回调OnclickListener中的Onclick方法
public Snackbar setAction(CharSequence text, final View.OnClickListener listener)
// 用于设定Action的字体颜色
public Snackbar setActionTextColor(@ColorInt int color)
// 设定提示的字体
public Snackbar setText(@NonNull CharSequence message)
// 展示SnackBar
public void show()
// 清除SnackBar
public void dismiss()
// 设置回调,比如OnDismissed或者OnShown
public Snackbar setCallback(Callback callback)
Snackbar需要一个控件容器view用来容纳,官方推荐使用CoordinatorLayout来确保Snackbar和其他组件的交互,比如滑动取消Snackbar、Snackbar出现时FloatingActionButton上移。举一个简单运用的例子:
[java] view plain copy print ?
- <android.support.percent.PercentRelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.administrator.singleinstance.MainActivity">
- <Button
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:id="@+id/btn"
- android:onClick="click"
- android:layout_centerInParent="true"
- app:layout_widthPercent="25%"
- app:layout_heightPercent="10%"
- android:text="取消"
- />
- <android.support.design.widget.CoordinatorLayout
- android:id="@+id/coor"
- android:layout_width="0dp"
- android:layout_alignParentBottom="true"
- app:layout_widthPercent="100%"
- android:layout_height="wrap_content">
- </android.support.design.widget.CoordinatorLayout>
- </android.support.percent.PercentRelativeLayout>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.singleinstance.MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/btn"
android:onClick="click"
android:layout_centerInParent="true"
app:layout_widthPercent="25%"
app:layout_heightPercent="10%"
android:text="取消"
/>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coor"
android:layout_width="0dp"
android:layout_alignParentBottom="true"
app:layout_widthPercent="100%"
android:layout_height="wrap_content">
</android.support.design.widget.CoordinatorLayout>
</android.support.percent.PercentRelativeLayout>
[java]
view plain
copy
print
?
- public void click(View view)
- Snackbar.make(coordinatorLayout, "确定取消吗?", Snackbar.LENGTH_LONG)
- .setAction("确定", new View.OnClickListener()
- @Override
- public void onClick(View view)
- Toast.makeText(MainActivity.this, "已经取消", Toast.LENGTH_SHORT).show();
- )
- .setCallback(new myOnClick())
- .show();
- /**
- * 滑动消失回调
- */
- public static final int DISMISS_EVENT_SWIPE = 0;
- /**
- * 点击消失回调
- */
- public static final int DISMISS_EVENT_ACTION = 1;
- /**
- * 超时回调
- */
- public static final int DISMISS_EVENT_TIMEOUT = 2;
- /**
- *调用Dismiss消失回调
- */
- public static final int DISMISS_EVENT_MANUAL = 3;
- /**
- * 再次出现消失SnackBar回调
- */
- public static final int DISMISS_EVENT_CONSECUTIVE = 4;
- class myOnClick extends Snackbar.Callback
- @Override
- public void onDismissed(Snackbar snackbar, int event)
- super.onDismissed(snackbar, event);
- switch (event)
- case DISMISS_EVENT_SWIPE:
- Logger.i("DISMISS_EVENT_SWIPE");
- break;
- case DISMISS_EVENT_ACTION:
- Logger.i("DISMISS_EVENT_ACTION");
- break;
- case DISMISS_EVENT_TIMEOUT:
- Logger.i("DISMISS_EVENT_TIMEOUT");
- break;
- case DISMISS_EVENT_MANUAL:
- Logger.i("DISMISS_EVENT_MANUAL");
- break;
- case DISMISS_EVENT_CONSECUTIVE:
- Logger.i("DISMISS_EVENT_CONSECUTIVE");
- break;
public void click(View view)
Snackbar.make(coordinatorLayout, "确定取消吗?", Snackbar.LENGTH_LONG)
.setAction("确定", new View.OnClickListener()
@Override
public void onClick(View view)
Toast.makeText(MainActivity.this, "已经取消", Toast.LENGTH_SHORT).show();
)
.setCallback(new myOnClick())
.show();
/**
* 滑动消失回调
*/
public static final int DISMISS_EVENT_SWIPE = 0;
/**
* 点击消失回调
*/
public static final int DISMISS_EVENT_ACTION = 1;
/**
* 超时回调
*/
public static final int DISMISS_EVENT_TIMEOUT = 2;
/**
*调用Dismiss消失回调
*/
public static final int DISMISS_EVENT_MANUAL = 3;
/**
* 再次出现消失SnackBar回调
*/
public static final int DISMISS_EVENT_CONSECUTIVE = 4;
class myOnClick extends Snackbar.Callback
@Override
public void onDismissed(Snackbar snackbar, int event)
super.onDismissed(snackbar, event);
switch (event)
case DISMISS_EVENT_SWIPE:
Logger.i("DISMISS_EVENT_SWIPE");
break;
case DISMISS_EVENT_ACTION:
Logger.i("DISMISS_EVENT_ACTION");
break;
case DISMISS_EVENT_TIMEOUT:
Logger.i("DISMISS_EVENT_TIMEOUT");
break;
case DISMISS_EVENT_MANUAL:
Logger.i("DISMISS_EVENT_MANUAL");
break;
case DISMISS_EVENT_CONSECUTIVE:
Logger.i("DISMISS_EVENT_CONSECUTIVE");
break;
这些运用都很简单,就不更多的嚼舌根了。有个花式使用SnackBar的连接,感兴趣的可以去看看:
http://www.jianshu.com/p/cd1e80e64311
2.TextInputLayout
TextInputLayout主要作用是作为EditText的容器,从而为EditText默认生成一个浮动的label,当用户点击了EditText之后,EditText中设置的Hint字符串会自动移到EditText的左上角。使用非常简单这有个例子写的不错:http://www.jcodecraeer.com/a/basictutorial/2015/0821/3338.html
getEditText()
得到控件中包含的 EditText 控件setError(CharSequence error)
设置错误信息并显示在 EditText 下方 应用场景:比如用户输错了密码或者用户名等-
setHint(CharSequence hint)
设置提示信息 -
setErrorEnabled(boolean enabled)
设置setError(CharSequence error)
这个函数是否可用 记住哦:这个函数一定要在setError(CharSequence error)
这个函数之后执行哦!
- <android.support.percent.PercentRelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.administrator.singleinstance.MainActivity">
- <android.support.design.widget.TextInputLayout
- android:id="@+id/input"
- app:layout_widthPercent="80%"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- app:errorEnabled="true"
- app:errorTextAppearance="@style/TextAppearance.Design.Error">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:imeOptions="actionGo"
- android:inputType="text"
- android:hint="输入姓名"
- android:lines="1"
- />
- </android.support.design.widget.TextInputLayout>
- <android.support.design.widget.TextInputLayout
- android:id="@+id/input2"
- android:layout_below="@+id/input"
- app:layout_widthPercent="80%"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- app:errorEnabled="true"
- app:errorTextAppearance="@style/TextAppearance.Design.Error">
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:imeOptions="actionGo"
- android:inputType="textPassword"
- android:hint="输入密码"
- android:lines="1"
- />
- </android.support.design.widget.TextInputLayout>
- </android.support.percent.PercentRelativeLayout>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.singleinstance.MainActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/input"
app:layout_widthPercent="80%"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:errorEnabled="true"
app:errorTextAppearance="@style/TextAppearance.Design.Error">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionGo"
android:inputType="text"
android:hint="输入姓名"
android:lines="1"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/input2"
android:layout_below="@+id/input"
app:layout_widthPercent="80%"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:errorEnabled="true"
app:errorTextAppearance="@style/TextAppearance.Design.Error">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionGo"
android:inputType="textPassword"
android:hint="输入密码"
android:lines="1"
/>
</android.support.design.widget.TextInputLayout>
</android.support.percent.PercentRelativeLayout>
[java]
view plain
copy
print
?
- public void TextInputLayout()
- textInputLayout = (TextInputLayout) findViewById(R.id.input);
- textInputLayout2 = (TextInputLayout) findViewById(R.id.input2);
- textInputLayout2.getEditText().addTextChangedListener(new TextWatcher()
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
- if (charSequence.length() < 4)
- textInputLayout2.setError("必须输入6个字符!");
- textInputLayout2.setErrorEnabled(true);
- else
- textInputLayout2.setErrorEnabled(false);
- @Override
- public void afterTextChanged(Editable editable)
- );
- textInputLayout.getEditText().addTextChangedListener(new TextWatcher()
- @Override
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
- @Override
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
- if (charSequence.length() < 4)
- textInputLayout.setError("必须输入4个字符!");
- textInputLayout.setErrorEnabled(true);
- else
- textInputLayout.setErrorEnabled(false);
- @Override
- public void afterTextChanged(Editable editable)
- );
public void TextInputLayout()
textInputLayout = (TextInputLayout) findViewById(R.id.input);
textInputLayout2 = (TextInputLayout) findViewById(R.id.input2);
textInputLayout2.getEditText().addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
if (charSequence.length() < 4)
textInputLayout2.setError("必须输入6个字符!");
textInputLayout2.setErrorEnabled(true);
else
textInputLayout2.setErrorEnabled(false);
@Override
public void afterTextChanged(Editable editable)
);
textInputLayout.getEditText().addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
if (charSequence.length() < 4)
textInputLayout.setError("必须输入4个字符!");
textInputLayout.setErrorEnabled(true);
else
textInputLayout.setErrorEnabled(false);
@Override
public void afterTextChanged(Editable editable)
);
3.TabLayout
TabLayout控件用于应用中轻松的添加Tab分组功能,总共有两种类型可选。1.固定的Tabs:对应的xml配置中的 app:tabMode="fixed"
2.可滑动的Tabs:对应xml配置中的 app:tabMode="scrollable"。
TabLayout,它就可以完成TabPageIndicator的效果,而且还是官方的,最好的是它可以兼容到2.2以上版本,包括2.2。接下来就简单使用一下。
先来布局:
[java] view plain copy print ?
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/activity_tab_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.administrator.designtest.TabLayoutActivity">
- <android.support.v4.view.ViewPager
- android:id="@+id/viewpager"
- android:layout_below="@+id/tablayout_top"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </android.support.v4.view.ViewPager>
- <android.support.design.widget.TabLayout
- android:id="@+id/tablayout_top"
- app:tabTextColor="#000"
- app:tabSelectedTextColor="#fff"
- android:background="@color/colorPrimary"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:tabMode="fixed"
- app:tabGravity="fill">
- </android.support.design.widget.TabLayout>
- </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.designtest.TabLayoutActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_below="@+id/tablayout_top"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout_top"
app:tabTextColor="#000"
app:tabSelectedTextColor="#fff"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</RelativeLayout>
这个很简单,再来一个适配器。
[java] view plain copy print ?
- public class ViewPagerAdapter extends FragmentPagerAdapter
- private List<BlankFragment>fragmentList;
- private List<String>titleList;
- public ViewPagerAdapter(FragmentManager fm, List<BlankFragment> fragmentList, List<String> titleList)
- super(fm);
- this.fragmentList = fragmentList;
- this.titleList = titleList;
- @Override
- public Fragment getItem(int position)
- return fragmentList.get(position);
- @Override
- public int getCount()
- return fragmentList.size();
- @Override
- public CharSequence getPageTitle(int position)
- return titleList.get(position);
public class ViewPagerAdapter extends FragmentPagerAdapter
private List<BlankFragment>fragmentList;
private List<String>titleList;
public ViewPagerAdapter(FragmentManager fm, List<BlankFragment> fragmentList, List<String> titleList)
super(fm);
this.fragmentList = fragmentList;
this.titleList = titleList;
@Override
public Fragment getItem(int position)
return fragmentList.get(position);
@Override
public int getCount()
return fragmentList.size();
@Override
public CharSequence getPageTitle(int position)
return titleList.get(position);
用过viewpager套Fragement的猿友都知道,就不啰嗦了。getPageTitle是获取需要显示的tab标题。新建一个fragment空的。
[java] view plain copy print ?
- public class BlankFragment extends Fragment
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState)
- return inflater.inflate(R.layout.fragment_blank, container, false);
public class BlankFragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
return inflater.inflate(R.layout.fragment_blank, container, false);
那么准备工作差不多了,开始进入主题,基本的介绍都加了注释
[java] view plain copy print ?
- public class TabLayoutActivity extends AppCompatActivity
- ViewPager viewPager;
- TabLayout tabLayout;
- List<BlankFragment>fragmentList;
- List<String>stringList;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_tab_layout);
- viewPager = (ViewPager) findViewById(R.id.viewpager);
- tabLayout = (TabLayout) findViewById(R.id.tablayout_top);
- //添加fragment
- fragmentList = new ArrayList<>();
- fragmentList.add(new BlankFragment());
- fragmentList.add(new BlankFragment());
- fragmentList.add(new BlankFragment());
- fragmentList.add(new BlankFragment());
- //添加标题
- stringList = new ArrayList<>();
- stringList.add("热门新闻");
- stringList.add("热门推荐");
- stringList.add("本月热榜");
- stringList.add("今日热榜");
- //添加tab
- tabLayout.addTab(tabLayout.newTab().setText("热门新闻"));
- tabLayout.addTab(tabLayout.newTab().setText("热门推荐"));
- tabLayout.addTab(tabLayout.newTab().setText("本月热榜"));
- tabLayout.addTab(tabLayout.newTab().setText("今日热榜"));
- //适配器
- ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),fragmentList,stringList);
- //建立联系
- viewPager.setAdapter(viewPagerAdapter);
- tabLayout.setupWithViewPager(viewPager,true);
public class TabLayoutActivity extends AppCompatActivity
ViewPager viewPager;
TabLayout tabLayout;
List<BlankFragment>fragmentList;
List<String>stringList;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_layout);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tablayout_top);
//添加fragment
fragmentList = new ArrayList<>();
fragmentList.add(new BlankFragment());
fragmentList.add(new BlankFragment());
fragmentList.add(new BlankFragment());
fragmentList.add(new BlankFragment());
//添加标题
stringList = new ArrayList<>();
stringList.add("热门新闻");
stringList.add("热门推荐");
stringList.add("本月热榜");
stringList.add("今日热榜");
//添加tab
tabLayout.addTab(tabLayout.newTab().setText("热门新闻"));
tabLayout.addTab(tabLayout.newTab().setText("热门推荐"));
tabLayout.addTab(tabLayout.newTab().setText("本月热榜"));
tabLayout.addTab(tabLayout.newTab().setText("今日热榜"));
//适配器
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),fragmentList,stringList);
//建立联系
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager,true);
tabLayout.setupWithViewPager(viewPager,true);这句代码是关联viewpager和tabLayout。后面的true是是否自动刷新fragment的布尔值,看源码就知道了。
[java] view plain copy print ? 《Android进阶之光》--Material Design
进阶篇Android学习笔记——TextInputLayout
进阶篇Android学习笔记——TextInputLayout
如果不需要,防止 CollapsingToolbarLayout 折叠