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

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


Widget NameDescription
android.support.design.widget.TextInputLayout强大带提示的MD风格的EditText
android.support.design.widget.FloatingActionButtonMD风格的圆形按钮,来自于ImageView
android.support.design.widget.Snackbar类似Toast,添加了简单的单个Action
android.support.design.widget.TabLayout选项卡
android.support.design.widget.NavigationViewDrawerLayout的SlideMenu
android.support.design.widget.CoordinatorLayout超级FrameLayout
android.support.design.widget.AppBarLayoutMD风格的滑动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.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.support.design.widget.FloatingActionButton

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


android.support.design.widget.TabLayout

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


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 联动



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值