天天看点

Android Design Support Library 中控件的使用简单介绍

Design Support Library包含8个控件,具体如下:

Widget Name Description
android.support.design.widget.TextInputLayout 强大带提示的MD风格的EditText
android.support.design.widget.FloatingActionButton MD风格的圆形按钮,来自于ImageView
android.support.design.widget.Snackbar 类似Toast,添加了简单的单个Action
android.support.design.widget.TabLayout 选项卡
android.support.design.widget.NavigationView DrawerLayout的SlideMenu
android.support.design.widget.CoordinatorLayout 超级FrameLayout
android.support.design.widget.AppBarLayout MD风格的滑动Layout
android.support.design.widget.CollapsingToolbarLayout 可折叠MD风格ToolbarLayout

android.support.design.widget.TextInputLayout 的使用

TextInputLayout  其就是一个组合控件

<android.support.design.widget.TextInputLayout
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_marginTop="10dp"
    android:layout_height="wrap_content">
    <EditText
        android:layout_width="match_parent"
        android:hint="请输入用户名"
        android:textSize="18dp"
        android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>      
TextInputLayout text = (TextInputLayout) findViewById(R.id.text);
text.setError("ddddddddddddddd");   设置错误消息
text.setErrorEnabled(true);     设置错误消息是否显示       
text.getEditText();         获取其内部的  EditText      
Android Design Support Library 中控件的使用简单介绍

android.support.design.widget.Snackbar

Snackbar 就是一个 提示控件,其类似于Toast 过一段时间就会自动消失,其用法和Toast类似

Snackbar.make(getWindow().getDecorView(), "Snackbar comes out", Snackbar.LENGTH_LONG)
        .setAction("Action", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(
                        MainActivity.this,
                        "Toast comes out",
                        Toast.LENGTH_SHORT).show();
            }
        }).show();      

参数一表示 , 该 弹出框依赖的 父布局, 也就是说弹出在哪一个父布局的底部

  参数二表示, 显示的内容

参数三表示 需要显示的时常

setAction 表示设置

Android Design Support Library 中控件的使用简单介绍

它的文本及点击事件

显示的效果

Android Design Support Library 中控件的使用简单介绍

android.support.design.widget.FloatingActionButton

FloatingActionButton就是一个悬浮的 按钮,其继承与 ImageView 和普通ImageView 一样的使用

Android Design Support Library 中控件的使用简单介绍

android.support.design.widget.TabLayout

通过选项卡的方式切换View并不是MD中才有的新概念,它们和顶层导航模式或者组织app中不同分组内容(比如,不同风格的音乐)是同一个概念。 Design library的TabLayout 既实现了固定的选项卡(View的宽度平均分配),也实现了可滚动的选项卡(View宽度不固定同时可以横向滚动)。如果你使用ViewPager在 tab之间横向切换,你可以直接从PagerAdapter的getPageTitle() 中创建选项卡,然后使用setupWithViewPager()将两者联系在一起。它可以使tab的选中事件能更新ViewPager,同时 ViewPager 的页面改变能更新tab的选中状态。 和github比较有名的 页签控件使用起来相差不是太大

Android Design Support Library 中控件的使用简单介绍

TabLayout 的一些方法

TabLayout table = (TabLayout) findViewById(R.id.table);
table.addTab(TabLayout.Tab tab, int position, boolean setSelected) 增加选项卡到 layout 中
table.addTab(TabLayout.Tab tab, boolean setSelected) 同上
table.addTab(TabLayout.Tab tab) 同上
table.getTabAt(int index) 得到选项卡
table.getTabCount() 得到选项卡的总个数
table.getTabGravity() 得到 tab 的 Gravity
table.getTabMode() 得到 tab 的模式
table.getTabTextColors() 得到 tab 中文本的颜色
table.newTab() 新建个 tab
table.removeAllTabs() 移除所有的 tab
table.removeTab(TabLayout.Tab tab) 移除指定的 tab
table.removeTabAt(int position) 移除指定位置的 tab
table.setOnTabSelectedListener(TabLayout.OnTabSelectedListener onTabSelectedListener) 为每个 tab 增加选择监听器
table.setScrollPosition(int position, float positionOffset, boolean updateSelectedText) 设置滚动位置
table.setTabGravity(int gravity) 设置 Gravity
table.setTabMode(int mode) 设置 Mode
table.setTabTextColors(ColorStateList textColor) 设置 tab 中文本的颜色
table.setTabTextColors(int normalColor, int selectedColor) 设置 tab 中文本的颜色 默认 选中
table.setTabsFromPagerAdapter(PagerAdapter adapter) 设置 PagerAdapter
table.setupWithViewPager(ViewPager viewPager) 和 ViewPager 联动