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
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN0LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90TQPp3YU90djRkT4FEVkZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39jN3ITOwADMyETNwYDM1EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
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 關聯