发布于 2017-10-10 10:47:46 | 131 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家介绍了学习使用Material Design控件的详细教程,Android实现标题栏自动缩放、放大效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文要实现内容移动时,标题栏自动缩放/放大的效果,效果如下:


控件介绍

这次需要用到得新控件比较多,主要有以下几个:

CoordinatorLayout
组织它的子views之间协作的一个Layout,它可以给子View切换提供动画效果。
AppBarLayout
可以让包含在其中的控件响应被标记了ScrollingViewBehavior的View的滚动事件
CollapsingToolbarLayout
可以控制包含在CollapsingToolbarLayout其中的控件,在响应collapse时是移除屏幕和固定在最上面
TabLayout
结合ViewPager,实现多个TAB的切换的功能
NestedScrollView
与ScrollView基本相同,不过包含在NestedScrollView中的控件移动时才能时AppBarLayout缩放

Layout布局


<?xml version=”1.0” encoding=”utf-8”?>
<android.support.design.widget.CoordinatorLayout xmlns:android=“http://schemas.android.com/apk/res/android”
  xmlns:app=“http://schemas.android.com/apk/res-auto”
  android:layout_width=“match_parent”
  android:layout_height=“match_parent”
  android:fitsSystemWindows=“true”>

  <android.support.design.widget.AppBarLayout
    android:layout_width=“match_parent”
    android:layout_height=“256dp”
    android:fitsSystemWindows=“true”
    android:theme=“@style/ThemeOverlay.AppCompat.Dark.ActionBar”>

    <android.support.design.widget.CollapsingToolbarLayout
      android:id=“@+id/collapsing_toolbar”
      android:layout_width=“match_parent”
      android:layout_height=“match_parent”
      android:fitsSystemWindows=“true”
      app:contentScrim=“?attr/colorPrimary”
      app:expandedTitleMarginEnd=“64dp”
      app:expandedTitleMarginStart=“48dp”
      app:layout_scrollFlags=“scroll|exitUntilCollapsed”>

      <ImageView
        android:id=“@+id/ivImage”
        android:layout_width=“match_parent”
        android:layout_height=“match_parent”
        android:fitsSystemWindows=“true”
        android:scaleType=“centerCrop”
        android:src=“@drawable/book1”
        app:layout_collapseMode=“parallax”
        app:layout_collapseParallaxMultiplier=“0.7” />

      <android.support.v7.widget.Toolbar
        android:id=“@+id/toolbar”
        android:layout_width=“match_parent”
        android:layout_height=“?attr/actionBarSize”
        app:layout_collapseMode=“pin”
        app:popupTheme=“@style/ThemeOverlay.AppCompat.Light” />


    </android.support.design.widget.CollapsingToolbarLayout>

  </android.support.design.widget.AppBarLayout>

  <LinearLayout
    android:layout_width=“match_parent”
    android:layout_height=“match_parent”
    android:orientation=“vertical”
    app:layout_behavior=“@string/appbar_scrolling_view_behavior”>

    <android.support.design.widget.TabLayout
      android:id=“@+id/sliding_tabs”
      android:layout_width=“match_parent”
      android:layout_height=“wrap_content”
      app:tabGravity=“fill”
      app:tabMode=“fixed” />

    <android.support.v4.view.ViewPager
      android:id=“@+id/viewpager”
      android:layout_width=“match_parent”
      android:layout_height=“match_parent” />
  </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

CollapsingToolbarLayout和TabLayout的使用说明可以参考探索新的Android Material Design支持库

代码实现


//Toolbar
 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 toolbar.setNavigationOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
     onBackPressed();
   }
 });

//使用CollapsingToolbarLayout后,title需要设置到CollapsingToolbarLayout上
 CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
 collapsingToolbar.setTitle("失控");

//设置ViewPager
 mViewPager = (ViewPager) findViewById(R.id.viewpager);
 setupViewPager(mViewPager);

//给TabLayout增加Tab, 并关联ViewPager
 TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
 tabLayout.addTab(tabLayout.newTab().setText("内容简介"));
 tabLayout.addTab(tabLayout.newTab().setText("作者简介"));
 tabLayout.addTab(tabLayout.newTab().setText("目录"));
 tabLayout.setupWithViewPager(mViewPager);

详细代码参见这里

项目源码已发布到Github,Material Design新控件基本介绍完了,
下篇文章会结合豆瓣读书的API,整合一下这些控件,做一个Demo。
源码地址:MaterialDesignExample

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务