Android 学习之《第一行代码》第二版 笔记(二十四)Material Design 实战 —— 下拉刷新和可折叠式标题栏

实现基础:
Android 学习之《第一行代码》第二版 笔记(二十三)Material Design 实战 —— 卡片式布局

一、下拉刷新

SwipeRefreshLayout 是用于实现下拉刷新功能的核心类,由support-v4库提供。将想要实现下拉刷新的控件放置到 SwipeRefreshLayout 当中,就可以迅速让这个控件支持下拉刷新。

1. 效果图

下拉式刷新

2. 代码

A.)activity_main.xml 将RecyclerView包裹进SwipeRefreshLayout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!--使用AppBarLayout解决覆盖问题
            第一步:将Toolbar嵌套到AppBarLayout中;
            第二步:给RecyclerView指定一个布局行为
        -->
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways|snap">
                <!--当AppBarLayout接收到滚动事件的时候,它内部的子控件可以指定如何去影响这些事件。
                    app:layout_scrollFlags属性:
                    scroll表示当RecyclerView向上滚动时,Toolbar会跟着向上滚动并实现隐藏
                    enterAlways表示当RecyclerView向下滚动时,Toolbar会跟着向下滚动并重新显示
                    snap表示当Toolbar还没有完全隐藏或显示时,会根据当前滚动的距离,自动选择隐藏还是显示
                -->
            </android.support.v7.widget.Toolbar>

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

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <!--使用layout_behavior指定布局行为-->

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </android.support.v7.widget.RecyclerView>

        </android.support.v4.widget.SwipeRefreshLayout>


        <!--app:elevation="8dp"设置悬浮按钮的悬浮高度为8dp,高度值越大,投影范围越大,投影效果越淡-->
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="16dp"
            android:src="@drawable/ic_done"
            app:elevation="8dp"/>

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_menu"
        app:headerLayout="@layout/nav_header">

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

</android.support.v4.widget.DrawerLayout>

B.)MainActivity.java 处理具体的刷新逻辑

import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;

    private SwipeRefreshLayout swipeRefresh;

    private Fruit[] fruits = {
            new Fruit("苹果",R.drawable.apple),
            new Fruit("香蕉",R.drawable.banana),
            new Fruit("橘子",R.drawable.orange),
            new Fruit("西瓜",R.drawable.watermelon),
            new Fruit("梨",R.drawable.pear),
            new Fruit("葡萄",R.drawable.grape),
            new Fruit("菠萝",R.drawable.pineapple),
            new Fruit("草莓",R.drawable.strawberry),
            new Fruit("樱桃",R.drawable.cherry),
            new Fruit("芒果",R.drawable.mango),
    };

    private List<Fruit> fruitList = new ArrayList<>();

    private FruitAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获得Toolbar实例
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //获得DrawerLayout实例
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        //获取NavigationView实例
        NavigationView navView = (NavigationView)findViewById(R.id.nav_view);
        //获得ActionBar实例
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null ){
            actionBar.setDisplayHomeAsUpEnabled(true);//显示导航按钮
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);//设置导航按钮图标
        }
        navView.setCheckedItem(R.id.nav_call);
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                mDrawerLayout.closeDrawers();
                return true;
            }
        });
        //获取FloatingActionButton实例
        FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //调用Snackbar的make()方法来创建一个Snackbar对象
                //make()方法第一个参数为当前界面布局的任意一个View;
                //第二个参数为Snackbar中显示的内容;
                //第三个参数为Snackbar显示的时长。
                Snackbar.make(view,"删除数据",Snackbar.LENGTH_SHORT)//调用setAction()方法设置动作,使之可以和用户进行交互
                        .setAction("复原",new View.OnClickListener(){
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(MainActivity.this,"数据恢复啦",Toast.LENGTH_SHORT).show();
                            }
                        }).show();
            }
        });
        initFruits();
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        //RecyclerView 内置的布局排列方式,网格布局排列方式,参数一:Context;参数二:列数
        GridLayoutManager layoutManager = new GridLayoutManager(MainActivity.this,2);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);

        //获取SwipeRefreshLayout实例
        swipeRefresh = (SwipeRefreshLayout)findViewById(R.id.swipe_refresh);
        //为下拉刷新进度条设置颜色
        swipeRefresh.setColorSchemeResources(R.color.colorPrimary);
        //监听下拉刷新
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshFruits();
            }
        });
    }

    //本地刷新
    private void refreshFruits(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    //将线程沉睡2s是因为本地刷新的速度非常快,为了体现刷新过程。
                    Thread.sleep(2000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //初始化水果数据
                        initFruits();
                        //通知数据发送变化
                        adapter.notifyDataSetChanged();
                        //表示刷新事件结束,并隐藏刷新进度条
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

    //进行水果的初始化,随机挑选五十个水果
    private void initFruits(){
        fruitList.clear();
        for (int i = 0 ; i < 50 ; i++){
            Random random = new Random();
            int index = random.nextInt(fruits.length);
            fruitList.add(fruits[index]);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
            case R.id.backup:
                Toast.makeText(this,"你点击了备份",Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this,"你点击了删除",Toast.LENGTH_SHORT).show();
                break;
            case R.id.settings:
                Toast.makeText(this,"你点击了设置",Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}

二、可折叠式标题栏

借助 CollapsingToolbarLayout 实现一个可折叠式标题栏的效果。Design Support 库提供。CollapsingToolbarLayout 不能独立存在,只能作为AppBarLayout 的直接子布局,而AppBarLayout 又必须是 CoordinatorLayout 的子布局。

1. 效果图

可折叠式标题栏1
可折叠式标题栏2
可折叠式标题栏3

2. 代码

新建活动FruitActivity(FruitActivity.java/activity_fruit.xml)
A.)activity_fruit.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FruitActivity">
    <!--CoordinatorLayout
      加强版FrameLayout,可以监听其所有子控件的各种事件,然后自动帮助我们做出最为合理的响应.
    -->

    <!--标题栏部分-->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="250dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <!--新布局:CollapsingToolbarLayout
                android:theme属性指定主题
                app:contentScrim属性指定CollapsingToolbarLayout在趋于折叠状态以及折叠之后的背景色
                (CollapsingToolbarLayout折叠之后就是一个普通的Toolbar)
                app:layout_scrollFlags属性:
                    scroll表示CollapsingToolbarLayout会随着水果内容详情的滚动一起滚动
                    exitUntilCollapsed表示当CollapsingToolbarLayout随着滚动完成折叠之后会保留在界面上不再移除屏幕
            -->

            <ImageView
                android:id="@+id/fruit_image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin">

            </android.support.v7.widget.Toolbar>
            <!--这个标题栏由普通的标题栏加上图片组成-->
            <!--app:layout_collapseMode属性
                用于指定当前控件在CollapsingToolbarLayout折叠过程中的折叠模式
                    pin:折叠过程中位置始终保持不变
                    parallax:折叠过程中产生一定的错位偏移
            -->
        </android.support.design.widget.CollapsingToolbarLayout>

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

    <!--水果内容详情-->
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <!--NestedScrollView允许使用滚动的方式来查看屏幕以外的数据,并且可以嵌套响应滚动事件的功能-->
        <!--由于CoordinatorLayout本身可以响应滚动事件
            因此在它的内部就需要使用NestedScrollView或者RecyclerView这样的布局
        -->

        <!--不管是ScrollView还是NestedScrollView内部都只允许存在一个直接子布局,
            因此,如果要放入很多东西,需要先嵌套一个LinearLayout,再在LinearLayout中放入具体内容
        -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="35dp"
                app:cardCornerRadius="4dp">

                <TextView
                    android:id="@+id/fruit_content_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp" />

            </android.support.v7.widget.CardView>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <!--添加评论悬浮按钮-->
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_comment"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|end" />
        <!--app:layout_anchor属性设置锚点
            使悬浮按钮出现在水果标题栏区域内
            app:layout_anchorGravity属性将其设置在区域右下角
        -->

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

B.)FruitActivity.java

import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;


public class FruitActivity extends AppCompatActivity {

    public static final String FRUIT_NAME = "fruit_name";

    public static final String FRUIT_IMAGE_ID = "fruit_image_id";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruit);
        //通过Intent获取传入的水果名称和水果图片资源ID
        Intent intent = getIntent();
        String fruitName = intent.getStringExtra(FRUIT_NAME);
        int fruitImageId = intent.getIntExtra(FRUIT_IMAGE_ID,0);
        //获取各个控件实例
        CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
        ImageView fruitImageView = (ImageView)findViewById(R.id.fruit_image_view);
        TextView fruitContentText = (TextView)findViewById(R.id.fruit_content_text);
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        //Toolbar标准用法
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        //将水果名称设置成当前界面的标题
        collapsingToolbar.setTitle(fruitName);
        //使用Glide加载传入的水果图片,并设置到ImageView
        Glide.with(FruitActivity.this).load(fruitImageId).into(fruitImageView);
        //填充水果内容详情
        String fruitContent = generateFruitContent(fruitName);
        fruitContentText.setText(fruitContent);
    }

    private String generateFruitContent(String fruitName){
        StringBuilder fruitContent = new StringBuilder();
        for (int i = 0 ; i < 500 ; i++){
            fruitContent.append(fruitName);
        }
        return fruitContent.toString();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //处理点击HomeAsUp按钮的点击事件,关闭当前活动,返回上一活动
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

C.)FruitAdapter.java

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {

    private Context mContext;

    private List<Fruit> mFruitList;

    //静态内部类,用于缓存子项控件实例
    static class ViewHolder extends RecyclerView.ViewHolder{
        CardView cardView;
        ImageView fruitImage;
        TextView fruitName;
        //静态内部类的构造函数,参数view通常就是RecyclerView子项的最外层布局
        public ViewHolder (View view){
            super(view);
            cardView = (CardView) view;
            fruitImage = (ImageView)view.findViewById(R.id.fruit_image);
            fruitName = (TextView)view.findViewById(R.id.fruit_name);
        }
    }

    //构造函数
    public FruitAdapter(List<Fruit> fruitList){
        mFruitList = fruitList;
    }

    //用于告诉RecyclerView一共有多少子项,直接返回数据源的长度即可
    @Override
    public int getItemCount() {
        return mFruitList.size();
    }

    //用于对RecyclerView子项的数据进行赋值,会在每个子项被滚动到屏幕内的时候执行。
    @Override
    public void onBindViewHolder(FruitAdapter.ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.fruitName.setText(fruit.getName());
        //使用Glide加载水果图片 Glide在内部有许多复杂的逻辑结构,包括图片压缩,防止内存溢出
        //Glide 用法:
        //首先调用Glide的with()方法,传入一个Context/Activity/Fragment参数
        //然后调用load()方法加载图片,传入一个URL地址或者本地路径或一个资源id
        //最后调用into()方法设置到具体某一个ImageView即可
        Glide.with(mContext).load(fruit.getImageId()).into(holder.fruitImage);
    }

    //用于创建ViewHolder实例
    @Override
    public FruitAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mContext == null){
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fruit_item,parent,false);
        final ViewHolder holder = new ViewHolder(view);
        //给CardView注册一个监听器
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mFruitList.get(position);
                Intent intent = new Intent(mContext,FruitActivity.class);
                intent.putExtra(FruitActivity.FRUIT_NAME,fruit.getName());
                intent.putExtra(FruitActivity.FRUIT_IMAGE_ID,fruit.getImageId());
                mContext.startActivity(intent);
            }
        });
        return holder;
    }
}

D.) 添加的图片 drawable-xhdpi
ic_comment.png (此处有图片)
ic_comment.png

三、充分利用系统状态栏空间 使背景图和状态栏融合

1. 效果图

充分利用系统状态栏空间

2. 代码

A.)activity_fruit.xml (android:fitsSystemWindows=“true”)

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FruitActivity"
    android:fitsSystemWindows="true">
    <!--CoordinatorLayout
      加强版FrameLayout,可以监听其所有子控件的各种事件,然后自动帮助我们做出最为合理的响应.
    -->

    <!--标题栏部分-->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true">
            <!--新布局:CollapsingToolbarLayout
                android:theme属性指定主题
                app:contentScrim属性指定CollapsingToolbarLayout在趋于折叠状态以及折叠之后的背景色
                (CollapsingToolbarLayout折叠之后就是一个普通的Toolbar)
                app:layout_scrollFlags属性:
                    scroll表示CollapsingToolbarLayout会随着水果内容详情的滚动一起滚动
                    exitUntilCollapsed表示当CollapsingToolbarLayout随着滚动完成折叠之后会保留在界面上不再移除屏幕
            -->

            <ImageView
                android:id="@+id/fruit_image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:fitsSystemWindows="true"/>
                <!--将ImageView及其所有父布局都设置android:fitsSystemWindows="true"-->
            
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin">

            </android.support.v7.widget.Toolbar>
            <!--这个标题栏由普通的标题栏加上图片组成-->
            <!--app:layout_collapseMode属性
                用于指定当前控件在CollapsingToolbarLayout折叠过程中的折叠模式
                    pin:折叠过程中位置始终保持不变
                    parallax:折叠过程中产生一定的错位偏移
            -->
        </android.support.design.widget.CollapsingToolbarLayout>

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

    <!--水果内容详情-->
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <!--NestedScrollView允许使用滚动的方式来查看屏幕以外的数据,并且可以嵌套响应滚动事件的功能-->
        <!--由于CoordinatorLayout本身可以响应滚动事件
            因此在它的内部就需要使用NestedScrollView或者RecyclerView这样的布局
        -->

        <!--不管是ScrollView还是NestedScrollView内部都只允许存在一个直接子布局,
            因此,如果要放入很多东西,需要先嵌套一个LinearLayout,再在LinearLayout中放入具体内容
        -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginTop="35dp"
                app:cardCornerRadius="4dp">

                <TextView
                    android:id="@+id/fruit_content_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp" />

            </android.support.v7.widget.CardView>

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <!--添加评论悬浮按钮-->
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:src="@drawable/ic_comment"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|end" />
        <!--app:layout_anchor属性设置锚点
            使悬浮按钮出现在水果标题栏区域内
            app:layout_anchorGravity属性将其设置在区域右下角
        -->

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

B.)在程序主题中将状态栏颜色指定成透明色
创建文件夹values-v21,新建Values resource file
styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--定义一个FruitActivityTheme主题,专门给FruitActivity使用-->
    <style name="FruitActivityTheme" parent="AppTheme">
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

</resources>

由于Android 5.0 之前的系统无法识别 FruitActivityTheme 这个主题,因此需要对values/styles.xml 文件进行修改

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="FruitActivityTheme" parent="AppTheme">
    </style>
</resources>

让FruitActivity使用这个主题

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.thinkpad.materialtest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name=".FruitActivity"
            android:theme="@style/FruitActivityTheme">
        </activity>
    </application>

</manifest>

整理学习自郭霖大佬的《第一行代码》
目前小白一名,持续学习Android中,如有错误请批评指正!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值