在Google提供的控件中,在support-design及v4,v7包中,存在着很多符合MD标准的控件,这里罗列出一些常用的控件

TextInputLayout

这个控件在作为输入框的时候是极其方便及好用的,结合EditText使用

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:orientation="vertical">
  7. <android.support.design.widget.TextInputLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. app:counterEnabled="true"
  11. app:counterMaxLength="10">
  12. <EditText
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:hint="请输入用户名" />
  16. </android.support.design.widget.TextInputLayout>
  17. <EditText
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:hint="请输入密码" />
  21. <Button
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:text="登录" />
  25. </LinearLayout>

效果

SearchView

搜索功能,位于ActionBar的位置

首先需要编写menu布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. tools:context=".MainActivity">
  6. <item android:id="@+id/menu_search"
  7. android:orderInCategory="100"
  8. app:showAsAction="always"
  9. app:actionViewClass="android.support.v7.widget.SearchView"
  10. android:title="search"/>
  11. <item android:id="@+id/menu_share"
  12. android:orderInCategory="100"
  13. app:showAsAction="ifRoom"
  14. android:icon="@android:drawable/ic_menu_share"
  15. android:title="share" />
  16. </menu>

然后复写onCreateOptionsMenu()方法

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. }
  7. @Override
  8. public boolean onCreateOptionsMenu(Menu menu) {
  9. getMenuInflater().inflate(R.menu.main, menu);
  10. MenuItem menuItem = menu.findItem(R.id.menu_search);
  11. final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
  12. //显示搜索框
  13. //searchView.setIconified(false);
  14. //显示搜索框,且其不能被隐藏
  15. searchView.setIconifiedByDefault(false);
  16. //显示提交按钮,这里可以获取到id,设置自定义图片
  17. ImageView icon = (ImageView) searchView.findViewById(R.id.search_go_btn);
  18. icon.setImageResource(R.mipmap.ic_launcher);
  19. icon.setVisibility(View.VISIBLE);
  20. //设置监听
  21. icon.setOnClickListener(new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. Toast.makeText(MainActivity.this, searchView.getQuery(), Toast.LENGTH_SHORT).show();
  25. }
  26. });
  27. //获取id,从而设置提示
  28. EditText edit = (EditText) searchView.findViewById(R.id.search_src_text);
  29. edit.setHint("请输入搜索的内容");
  30. searchView.setSubmitButtonEnabled(true);
  31. //SearchView还有很多设置监听选项,例如提交监听,文本监听
  32. return true;
  33. }
  34. }

效果如下:

Toolbar

顶部导航栏。用于显示标题,返回,菜单等,最开始是使用的ActionBar

由于ActionBar的种种使用不便,再加上拓展性差,google后来推出了Toolbar,为了增强其功能,现在还有APPbar可供使用

首先还是要把主题设置为NoActionBar

  1. <resources>
  2. <!-- Base application theme. -->
  3. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  4. <!-- Customize your theme here. -->
  5. <item name="colorPrimary">@color/colorPrimary</item>
  6. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  7. <item name="colorAccent">@color/colorAccent</item>
  8. </style>
  9. </resources>

然后使用Toolbar布局,由于ToolBar继承自ViewGroup,所以其是一个容器

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <android.support.v7.widget.Toolbar
  9. android:id="@+id/toolbar"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:background="?attr/colorPrimary"
  13. app:logo="@mipmap/ic_launcher"
  14. app:navigationIcon="@drawable/abc_ic_ab_back_material"
  15. app:subtitle="子标题"
  16. app:subtitleTextColor="@android:color/white"
  17. app:title="主标题"
  18. app:titleTextColor="@color/colorAccent">
  19. <!--<TextView-->
  20. <!--android:layout_width="wrap_content"-->
  21. <!--android:layout_height="wrap_content"-->
  22. <!--android:layout_gravity="center"-->
  23. <!--android:text="这里是标题"/>-->
  24. </android.support.v7.widget.Toolbar>
  25. </LinearLayout>

最后设置Toolbar到活动,并设置监听

  1. public class MainActivity extends AppCompatActivity {
  2. private Toolbar toolbar;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. toolbar = findViewById(R.id.toolbar);
  8. setSupportActionBar(toolbar);
  9. toolbar.setNavigationOnClickListener(new View.OnClickListener() {
  10. @Override
  11. public void onClick(View v) {
  12. finish();
  13. }
  14. });
  15. }
  16. }

测试效果如下

接下来实现Toolbar的隐藏效果

这样的效果实现思路就是Toolbar在上层,后面的布局在下层,后面的布局设置padding,然后再滑动过程中监听滑动距离,设置Toolbar的透明度

  1. interface TranslucentListener {
  2. //透明度的回调监听 alpha:0~1
  3. void onTranlucent(float alpha);
  4. }

自定义ScrollView,在onScrollChanged()中监听

  1. public class MyScrollView extends ScrollView {
  2. private TranslucentListener translucentListener;
  3. public void setTranslucentListener(TranslucentListener translucentListener) {
  4. this.translucentListener = translucentListener;
  5. }
  6. public MyScrollView(Context context, AttributeSet attrs) {
  7. super(context, attrs);
  8. }
  9. @Override
  10. protected void onScrollChanged(int l, int t, int oldl, int oldt) {
  11. super.onScrollChanged(l, t, oldl, oldt);
  12. if (translucentListener != null) {
  13. Log.d("cj5785", "onScrollChanged");
  14. int scrollY = getScrollY();
  15. int screen_height = getContext().getResources().getDisplayMetrics().heightPixels;
  16. //滑出1/3时候完全透明
  17. if (scrollY <= screen_height / 3f) {
  18. translucentListener.onTranlucent(1 - scrollY / (screen_height / 3f));
  19. }
  20. }
  21. }
  22. }

最后在活动中回调

  1. public class MainActivity extends AppCompatActivity implements TranslucentListener{
  2. private Toolbar toolbar;
  3. private MyScrollView scrollView;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. toolbar = findViewById(R.id.toolbar);
  9. setSupportActionBar(toolbar);
  10. toolbar.setNavigationOnClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View v) {
  13. finish();
  14. }
  15. });
  16. scrollView = findViewById(R.id.scroll_view);
  17. scrollView.setTranslucentListener(this);
  18. }
  19. @Override
  20. public void onTranlucent(float alpha) {
  21. toolbar.setAlpha(alpha);
  22. }
  23. }

贴出布局

android:clipToPadding="false" 该控件的绘制范围是否不在Padding里面。false:绘制的时候范围会考虑padding即会往里面缩进

android:clipChildren="false" 子控件是否能不超出padding的区域(比如ScrollView上滑动的时候,child可以滑出该区域)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <com.cj5785.toolbartest.MyScrollView
  9. android:id="@+id/scroll_view"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:clipChildren="false"
  13. android:clipToPadding="false"
  14. android:paddingTop="?attr/actionBarSize">
  15. <android.support.v7.widget.LinearLayoutCompat
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent"
  18. android:orientation="vertical">
  19. <TextView
  20. android:layout_width="match_parent"
  21. android:layout_height="200dp"
  22. android:background="@android:color/holo_orange_light" />
  23. <TextView
  24. android:layout_width="match_parent"
  25. android:layout_height="200dp"
  26. android:background="@android:color/holo_green_light" />
  27. <TextView
  28. android:layout_width="match_parent"
  29. android:layout_height="200dp"
  30. android:background="@android:color/holo_blue_light" />
  31. <TextView
  32. android:layout_width="match_parent"
  33. android:layout_height="200dp"
  34. android:background="@android:color/holo_red_light" />
  35. <TextView
  36. android:layout_width="match_parent"
  37. android:layout_height="200dp"
  38. android:background="@android:color/holo_purple" />
  39. </android.support.v7.widget.LinearLayoutCompat>
  40. </com.cj5785.toolbartest.MyScrollView>
  41. <android.support.v7.widget.Toolbar
  42. android:id="@+id/toolbar"
  43. android:layout_width="match_parent"
  44. android:layout_height="?attr/actionBarSize"
  45. android:background="?attr/colorPrimary"
  46. app:logo="@mipmap/ic_launcher"
  47. app:navigationIcon="@drawable/abc_ic_ab_back_material"
  48. app:subtitle="子标题"
  49. app:subtitleTextColor="@android:color/white"
  50. app:title="主标题"
  51. app:titleTextColor="@color/colorAccent">
  52. <!--<TextView-->
  53. <!--android:layout_width="wrap_content"-->
  54. <!--android:layout_height="wrap_content"-->
  55. <!--android:layout_gravity="center"-->
  56. <!--android:text="这里是标题"/>-->
  57. </android.support.v7.widget.Toolbar>
  58. </RelativeLayout>

实现效果:

CoordinatorLayout

监听滑动控件的滑动通过Behavior反馈到其他子控件并执行一些动画

这里的滑动控件指的是RecyclerView/NestedScrollView/ViewPager,意味着ListViewScrollView不行

使用前需要添加support-design依赖

  1. implementation 'com.android.support:design:25.4.0'

布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <android.support.v7.widget.RecyclerView
  9. android:id="@+id/recycler_view"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent" />
  12. <android.support.design.widget.FloatingActionButton
  13. app:layout_behavior=".FABBehavior"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="bottom|end"
  17. android:layout_margin="16dp" />
  18. </android.support.design.widget.CoordinatorLayout>

适配器

  1. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
  2. private List<String> list;
  3. class MyViewHolder extends RecyclerView.ViewHolder {
  4. private TextView textView;
  5. public MyViewHolder(View itemView) {
  6. super(itemView);
  7. textView = (TextView) itemView.findViewById(android.R.id.text1);
  8. }
  9. }
  10. public MyAdapter(List<String> list) {
  11. this.list = list;
  12. }
  13. @Override
  14. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  15. LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
  16. View view = layoutInflater.inflate(android.R.layout.simple_list_item_1,parent,false);
  17. MyViewHolder holder = new MyViewHolder(view);
  18. return holder;
  19. }
  20. @Override
  21. public void onBindViewHolder(MyViewHolder holder, int position) {
  22. holder.textView.setText(list.get(position));
  23. }
  24. @Override
  25. public int getItemCount() {
  26. return list.size();
  27. }
  28. }

Behavior

  1. public class FABBehavior extends FloatingActionButton.Behavior {
  2. private boolean isShow;
  3. public FABBehavior(Context context, AttributeSet attrs) {
  4. super(context, attrs);
  5. }
  6. //依赖滑动开始回调
  7. @Override
  8. public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
  9. //nestedScrollAxes 滑动的方向,这里我们依赖recyclerview,只关心其是否垂直滑动
  10. return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
  11. || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
  12. }
  13. //依赖滑动过程回调
  14. @Override
  15. public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
  16. super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
  17. //根据情况执行动画
  18. if (dyConsumed > 0 && isShow) {
  19. isShow = false;
  20. onHide(child);
  21. } else if (dyConsumed < 0) {
  22. isShow = true;
  23. onShow(child);
  24. }
  25. }
  26. private void onShow(FloatingActionButton fab) {
  27. ViewCompat.animate(fab).scaleX(1f).scaleY(1f).start();
  28. }
  29. private void onHide(FloatingActionButton fab) {
  30. ViewCompat.animate(fab).scaleX(0f).scaleY(0f).start();
  31. }
  32. }

调用

  1. public class MainActivity extends AppCompatActivity {
  2. private List<String> list = new ArrayList<>();
  3. private RecyclerView recyclerView;
  4. private MyAdapter adapter;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  10. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  11. for (int i = 0; i < 100; i++) {
  12. list.add("item " + i);
  13. }
  14. adapter = new MyAdapter(list);
  15. recyclerView.setAdapter(adapter);
  16. }
  17. }

效果如下,实现了FloatingActionButton上划隐藏,下拉显示

AppBarLayout

AppBarLayout继承自LinearLayout,一般用于导航栏,其常见子控件为Toolbar,但同时又不局限于Toolbar,可以实现导航栏的多种综合效果,其作为容器,里面可以增加布局,按照需要去实现各种效果

这里做一个简单的演示,导航栏通过recyclerview的滑动而显隐的效果

首先依旧是要引入依赖

  1. implementation 'com.android.support:design:25.4.0'

然后编写布局,其外层为CoordinatorLayout,这里使用了预设behavior:appbar_scrolling_view_behavior,在监听的控件加上Flag设置,例如这里的app:layout_scrollFlags="scroll|enterAlways""

其Flag参数包括

scroll:将此布局和滚动时间关联。这个标识要设置在其他标识之前,没有这个标识则布局不会滚动且其他标识设置无效

enterAlways:任何向下滚动操作都会使此布局可见。这个标识通常被称为快速返回模式

enterAlwaysCollapsed:假设你定义了一个最小高度(minHeight)同时enterAlways也定义了,那么view将在到达这个最小高度的时候开始显示,并且从这个时候开始慢慢展开,当滚动到顶部的时候展开完

exitUntilCollapsed:当你定义了一个minHeight,此布局将在滚动到达这个最小高度的时候折叠

snap:当一个滚动事件结束,如果视图是部分可见的,那么它将被滚动到收缩或展开。例如,如果视图只有底部25%显示,它将折叠。相反,如果它的底部75%可见,那么它将完全展开

snap:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <android.support.v7.widget.RecyclerView
  9. android:id="@+id/recycler_view"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:clipChildren="false"
  13. android:clipToPadding="false"
  14. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  15. <android.support.design.widget.AppBarLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content">
  18. <android.support.v7.widget.Toolbar
  19. android:layout_width="match_parent"
  20. android:layout_height="?attr/actionBarSize"
  21. android:layout_gravity="center"
  22. android:background="?attr/colorPrimary"
  23. app:layout_scrollFlags="scroll|enterAlways"
  24. app:title="这是标题" />
  25. </android.support.design.widget.AppBarLayout>
  26. </android.support.design.widget.CoordinatorLayout>

由于使用的是RecyclerView,故还需要适配器

  1. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
  2. private List<String> list;
  3. class MyViewHolder extends RecyclerView.ViewHolder {
  4. private TextView textView;
  5. public MyViewHolder(View itemView) {
  6. super(itemView);
  7. textView = (TextView) itemView.findViewById(android.R.id.text1);
  8. }
  9. }
  10. public MyAdapter(List<String> list) {
  11. this.list = list;
  12. }
  13. @Override
  14. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  15. LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
  16. View view = layoutInflater.inflate(android.R.layout.simple_list_item_1,parent,false);
  17. MyViewHolder holder = new MyViewHolder(view);
  18. return holder;
  19. }
  20. @Override
  21. public void onBindViewHolder(MyViewHolder holder, int position) {
  22. holder.textView.setText(list.get(position));
  23. }
  24. @Override
  25. public int getItemCount() {
  26. return list.size();
  27. }
  28. }

设置RecyclerView数据

  1. public class MainActivity extends AppCompatActivity {
  2. private List<String> list = new ArrayList<>();
  3. private MyAdapter adapter;
  4. private RecyclerView recyclerView;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  10. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  11. for (int i = 0; i < 30; i++) {
  12. list.add("item " + i);
  13. }
  14. adapter = new MyAdapter(list);
  15. recyclerView.setAdapter(adapter);
  16. }
  17. }

查看效果如下

ViewPager + TabLayout + Fragment + AppBarLayout

AppBarLayout同时还可以使用其他可滑动控件,例如NestedScrollView,其常用组合为:ViewPager + TabLayout + Fragment + AppBarLayout

由于这套组合的体验效果不错,这里展现一下这个样式的demo

首先依旧是引入依赖


因为使用了Toolbar,所以这里使用NoActionBar的主题

  1. <resources>
  2. <!-- Base application theme. -->
  3. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  4. <!-- Customize your theme here. -->
  5. <item name="colorPrimary">@color/colorPrimary</item>
  6. <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  7. <item name="colorAccent">@color/colorAccent</item>
  8. </style>
  9. </resources>

编写ViewPager的Fragment布局,这里简单显示text,记得这里应该是一个滑动控件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <android.support.v4.widget.NestedScrollView
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  11. <TextView
  12. android:id="@+id/text_view"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:textColor="@android:color/black"
  16. android:textSize="24sp" />
  17. </android.support.v4.widget.NestedScrollView>
  18. </LinearLayout>

然后是自定义的fragment

  1. public class DefaultFragment extends Fragment {
  2. @Nullable
  3. @Override
  4. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  5. View view = inflater.inflate(R.layout.fragment_layout,container,false);
  6. TextView textView = (TextView) view.findViewById(R.id.text_view);
  7. Bundle bundle = getArguments();
  8. String title = bundle.getString("title");
  9. textView.setText(title+"\n\n");
  10. for (int i = 0; i < 10; i++) {
  11. textView.append("这是一个ViewPager + TabLayout + Fragment + AppBarLayout测试用例\n\n");
  12. }
  13. return view;
  14. }
  15. }

接下来是主布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <android.support.design.widget.AppBarLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. <android.support.v7.widget.Toolbar
  13. android:layout_width="match_parent"
  14. android:layout_height="?attr/actionBarSize"
  15. android:background="?attr/colorPrimary"
  16. app:layout_scrollFlags="scroll|enterAlways"
  17. app:title="标题" />
  18. <android.support.design.widget.TabLayout
  19. android:id="@+id/table_layout"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:background="@android:color/holo_orange_light"
  23. app:tabGravity="fill"
  24. app:tabMode="scrollable" />
  25. </android.support.design.widget.AppBarLayout>
  26. <android.support.v4.view.ViewPager
  27. android:id="@+id/view_pager"
  28. android:layout_width="match_parent"
  29. android:layout_height="match_parent"
  30. app:layout_behavior="@string/appbar_scrolling_view_behavior" />
  31. </android.support.design.widget.CoordinatorLayout>

最后关联TableLayout和ViewPager

  1. public class MainActivity extends AppCompatActivity {
  2. private TabLayout tabLayout;
  3. private ViewPager viewPager;
  4. private String[] title = {
  5. "新闻", "体育", "汽车", "科技", "手机", "数码", "读书", "艺术"
  6. };
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. tabLayout = (TabLayout) findViewById(R.id.table_layout);
  12. viewPager = (ViewPager) findViewById(R.id.view_pager);
  13. MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
  14. viewPager.setAdapter(adapter);
  15. tabLayout.setupWithViewPager(viewPager);
  16. }
  17. class MyPagerAdapter extends FragmentPagerAdapter {
  18. public MyPagerAdapter(FragmentManager fm) {
  19. super(fm);
  20. }
  21. @Override
  22. public CharSequence getPageTitle(int position) {
  23. return title[position];
  24. }
  25. @Override
  26. public Fragment getItem(int position) {
  27. Fragment fragment = new DefaultFragment();
  28. Bundle bundle = new Bundle();
  29. bundle.putString("title", title[position]);
  30. fragment.setArguments(bundle);
  31. return fragment;
  32. }
  33. @Override
  34. public int getCount() {
  35. return title.length;
  36. }
  37. }
  38. }

这样就完成了显示功能

CollapsingToolbarLayout

这个控件可以实现Toolbar的折叠效果

先看下其常用参数

expandedTitleMargin:展开后的边距

statusBarScrim:状态栏颜色

contentScrim:内容颜色

app:layout_collapseMode:设置折叠模式,分为视差模式(parallax:在折叠的时候会有折叠视差效果,一般搭配layout_collapseParallaxMultiplier,视差的明显程度),固定模式(pin:在折叠的时候最后固定在顶端,再和toolbar一起推出),无模式(none:折叠时toolbar直接推出)

expandedTitleGravity:展开的位置

collapsedTitleGravity:折叠的位置

使用时依然需要导入依赖

  1. implementation 'com.android.support:design:25.4.0'

然后使用NoActionBar主题

使用时的布局示例

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <android.support.v4.widget.NestedScrollView
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. app:layout_behavior="@string/appbar_scrolling_view_behavior">
  12. <TextView
  13. android:id="@+id/text_view"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:text="@string/test_text"
  17. android:textColor="@android:color/black"
  18. android:textSize="24sp" />
  19. </android.support.v4.widget.NestedScrollView>
  20. <android.support.design.widget.AppBarLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="180dp">
  23. <android.support.design.widget.CollapsingToolbarLayout
  24. android:layout_width="match_parent"
  25. android:layout_height="match_parent"
  26. app:layout_scrollFlags="scroll|exitUntilCollapsed"
  27. app:expandedTitleMargin="8dp"
  28. app:statusBarScrim="?attr/colorPrimary"
  29. app:contentScrim="?attr/colorPrimary"
  30. app:expandedTitleGravity="center"
  31. app:collapsedTitleGravity="center"
  32. app:title="现代诗歌欣赏">
  33. <ImageView
  34. android:layout_width="match_parent"
  35. android:layout_height="match_parent"
  36. android:scaleType="centerCrop"
  37. android:src="@drawable/tdj"
  38. app:layout_collapseMode="parallax"
  39. app:layout_collapseParallaxMultiplier="0.5"/>
  40. <android.support.v7.widget.Toolbar
  41. app:layout_collapseMode="pin"
  42. android:id="@+id/tool_bar"
  43. android:layout_width="match_parent"
  44. android:layout_height="?attr/actionBarSize"
  45. android:background="?attr/colorPrimary"/>
  46. </android.support.design.widget.CollapsingToolbarLayout>
  47. </android.support.design.widget.AppBarLayout>
  48. </android.support.design.widget.CoordinatorLayout>

设置Toolbar

  1. public class MainActivity extends AppCompatActivity {
  2. private Toolbar toolbar;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. toolbar = (Toolbar) findViewById(R.id.tool_bar);
  8. toolbar.setNavigationIcon(R.mipmap.ic_launcher);
  9. setSupportActionBar(toolbar);
  10. }
  11. }

实现的效果如下图

高级UI-符合MD的常用控件的更多相关文章

  1. [WinForm]WinForm跨线程UI操作常用控件类大全

    前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...

  2. UI常用控件

    UICommonlyUsedControls [UI常用控件] 不需要学习多么深入,但是要知道系统提供的有用的控件. 一.UISwitch(开关) 二.UIActivityIndicatorView( ...

  3. 【Android Studio】安卓开发初体验3.1——UI设计之常用控件

    常用控件 首先对xml文件的编辑有三种模式 Code为纯代码 Split是一边代码,一边预览效果图 Designer就是有UI设计界面 TextView 用于在界面上显示一段文本信息 所有控件都可以在 ...

  4. Day3 UI:7种常用控件、4种基本布局

    Android常用控件 TextView <TextView android:id="@+id/text_view" android:layout_width="m ...

  5. Xamarin Studio在Mac环境下的配置和Xamarin.iOS常用控件的示例

    看过好多帖子都是Win环境装XS,Mac只是个模拟器,讲解在Mac环境下如何配置Xamarin Studio很少,也是一点点找资料,东拼西凑才把Xamarin Studio装在Mac上跑起来,如下: ...

  6. B/S一些小知识及常用控件

    一: B/S网页的运行 页面在设计的时候,本身就是一个类.在运行的时间,是一个对象. 其中aspx和aspx.cs是在同一个类下. aspx是主要是负责界面,而aspx.cs主要是负责数据逻辑. 呈现 ...

  7. 第32讲 UI组件之 时间日期控件DatePicker和TimePicker

    第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置,    Time ...

  8. 第31讲 UI组件之 Gallery画廊控件

    第31讲 UI组件之 Gallery画廊控件 1.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery只 ...

  9. Windows Phone开发(10):常用控件(上)

    原文:Windows Phone开发(10):常用控件(上) Windows Phone的控件有几个来源,和传统的桌面应用程序开发或Web开发一样,有默认提供的控件和第三方开者发布的控件.一般而言,如 ...

随机推荐

  1. Java Part 001( 03_01_数据类型和运算符 )

    注释 Java语言的注释一共有三种类型,分别是单行注释.多行注释和文档注释. 1. 单行注释 单行注释就是在程序中注释一行代码,在Java语言中,使用双斜线“//”进行单行注释. 2. 多行注释 多行 ...

  2. 002_软件安装之_keil4与keil5共存

    目的:实现keil4和keil5的共存 1. Keil4 主要用来开发 C51 程序 2. Keil5 也就是 MDK 主要用来开发 ARM 芯片,如 STM32 系列芯片 3. 资料下载地址:链接: ...

  3. sql server join ,inner join ,left join ,right join 的使用

    测试数据脚本 CREATE TABLE Atable ( S# INT, Sname nvarchar(32), Sage INT, Sfrom nvarchar(8) ) insert into A ...

  4. oracle 备份恢复之recover database的四条语句区别

    1  recover database using backup controlfile2  recover database until cancel3  recover database usin ...

  5. package.json文件说明解释

    1.package.json是什么? 什么是Node.js的模块(Module)?在Node.js中,模块是一个库或框架,也是一个Node.js项目.Node.js项目遵循模块化的架构,当我们创建了一 ...

  6. [Luogu] 外星密码

    https://www.luogu.org/problemnew/show/P1928 沙比提 读清题目 #include <bits/stdc++.h> using namespace ...

  7. 差分约束 4416 FFF 团卧底的后宫

    /* 4416 FFF 团卧底的后宫  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 你在某日收到了 FFF ...

  8. D-【乐】k进制数(同余)

    题目 https://ac.nowcoder.com/acm/contest/907/D 做法 \((x)_k\)定义编号,如果\(a+b\)加到一起能进一位,\(a+b\rightarrow 1+( ...

  9. Mixed Content: The page at ‘https://XXX’ was loaded over HTTPS, but requested an insecure........

    iframe引入视频的文件的时候报这个错 其实只要改成 加上一个s就好了  ...

  10. Lasso回归的坐标下降法推导

    目标函数 Lasso相当于带有L1正则化项的线性回归.先看下目标函数:RSS(w)+λ∥w∥1=∑Ni=0(yi−∑Dj=0wjhj(xi))2+λ∑Dj=0∣wj∣RSS(w)+λ∥w∥1=∑i=0 ...