android 好看的标签栏,说说 Android 的 Material Design 设计(五)——可折叠式标题栏...

2023-11-11 02:40

本文主要是介绍android 好看的标签栏,说说 Android 的 Material Design 设计(五)——可折叠式标题栏...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 CollapsingToolbarLayout 布局

CollapsingToolbarLayout 是基于 Toolbar 的布局。它可以让 Toolbar 的效果变得更加华丽。

注意:CollapsingToolbarLayout 只能作为 AppBarLayout 的直接子布局。

现在我们创建一个空活动来显示猫的详情:

a8e7adcc189d?utm_source=oschina-app

然后在 activity_cat.xml 中编写界面布局,主要分为两个部分,一个是标题栏,另一个是内容:

xmlns:material="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/app_bar_layout"

android:layout_width="match_parent"

android:layout_height="250dp">

android:id="@+id/collapsing_toolbar_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

material:contentScrim="?attr/colorPrimary"

material:layout_scrollFlags="scroll|exitUntilCollapsed">

android:id="@+id/cat_image_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="centerCrop"

material:layout_collapseMode="parallax" />

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

material:layout_collapseMode="pin" />

android:layout_width="match_parent"

android:layout_height="match_parent"

material:layout_behavior="@string/appbar_scrolling_view_behavior">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

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"

material:cardCornerRadius="4dp">

android:id="@+id/cat_text_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="10dp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="16dp"

android:src="@drawable/comment"

material:layout_anchor="@id/app_bar_layout"

material:layout_anchorGravity="bottom|end" />

整体布局使用 CoordinatorLayout。

定义了 xmlns:material="http://schemas.android.com/apk/res-auto" 命名空间。

标题栏布局:

标题栏布局使用 AppBarLayout,内层嵌套了 CollapsingToolbarLayout。

CollapsingToolbarLayout 中的 contentScrim 可指定当标题栏趋于折叠以及折叠之后的背景色;而 layout_scrollFlags 中的 scroll 表示 CollapsingToolbarLayout 会随着内容的滚动而滚动; layout_scrollFlags 中的 exitUntilCollapsed 表示折叠后会保留在界面中(不会被移出屏幕)。

在 CollapsingToolbarLayout 中,定义了 ImageView 与 Toolbar。即这个标题栏是由普通标题栏加图片组合而成的。其中有一个 layout_collapseMode 属性,它用于设置折叠模式。pin 表示在折叠过程中使用保持,就像被大头针定住了一样;parallax 表示在折叠过程中会产生视差。

内容布局:

外层使用 NestedScrollView,它不仅支持使用滚动的方式来查看屏幕外的数据,还能嵌套响应滚动事件。我们还通过 layout_behavior 来指定布局行为。

因为 NestedScrollView 内部只允许存在一个直接的子布局,所以我们可以先嵌套一个 LinearLayout,然后再其内部放入更多内容。

这里使用 TextView 来显示内容,然后把它放入 CardView (卡片式布局)中。

我们为 CardView 设置了外边距,还设置了圆角。这些工作都是为了让页面变得更好看。

最后我们还加了一个悬浮按钮(FloatingActionButton):

首先把按钮图片 png ,放在 drawable 文件夹下。

通过 layout_anchor 来设置锚点,让悬浮按钮出现在标题栏区域内。

通过 layout_anchorGravity 让按钮出现在标题栏区域的右下角。

2 活动类

public class CatActivity extends AppCompatActivity {

/**

* 猫名

*/

public static final String CAT_NAME = "cat_name";

/**

* 猫图片 ID

*/

public static final String CAT_IMAGE_ID = "cat_image_id";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_cat);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

ActionBar actionBar = getSupportActionBar();

if (actionBar != null) {//启用 HomeAsUp 按钮(返回箭头)

actionBar.setDisplayHomeAsUpEnabled(true);

}

Intent intent = getIntent();

//设置标题

CollapsingToolbarLayout layout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);

String catName = intent.getStringExtra(CAT_NAME);

layout.setTitle(catName);

//设置图片

int catImageId = intent.getIntExtra(CAT_IMAGE_ID, 0);

ImageView imageView = (ImageView) findViewById(R.id.cat_image_view);

Glide.with(this).load(catImageId).into(imageView);//加载图片

//设置内容

TextView contentView = (TextView) findViewById(R.id.cat_text_view);

contentView.setText(generateContent(catName));

}

/**

* 生成内容

*

* @param catName 猫名

* @return

*/

private String generateContent(String catName) {

//实践中,会根据参数名称,返回对应的内容

return "俄罗斯蓝猫(Russian Blue),历史上曾被称做阿契安吉蓝猫,曾有过三种不同的名字。最初是阿契安吉蓝猫,直到20世纪40年代才有现在的名字,另外有段时间它则叫做马耳他猫。证据显示,这种猫确实原产于俄罗斯,因为已在俄罗斯寒带地区发现了同种的猫。俄罗斯蓝猫体型细长,大而直立的尖耳朵,脚掌小而圆,走路像是用脚尖在走。身上披着银蓝色光泽的短被毛,配上修长苗条的体型和轻盈的步态,尽显一派猫中的贵族风度。";

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()){

case android.R.id.home://点击 HomeAsUp 按钮时

finish();//关闭当前活动(即返回上一个活动)

return true;

}

return super.onOptionsItemSelected(item);

}

}

我们在标题栏启用 HomeAsUp 按钮(返回箭头),用于关闭当前活动。

将 CollapsingToolbarLayout 传递过来的猫类型,作为标题。

接着使用 Glide 来加载图片。

然后设置内容。

最后在 onOptionsItemSelected 中处理 HomeAsUp 按钮的点击事件。

现在我们要处理首页活动 RecyclerView 的点击事件,当点击某只猫时,打开它的详情活动页:

public class CatAdapter extends RecyclerView.Adapter {

private Context context;

private List cats = new ArrayList<>();

public CatAdapter(List cats) {

this.cats = cats;

}

@Override

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

if (context == null) {//设置上下文环境

context = parent.getContext();

}

View view = LayoutInflater.from(context).inflate(R.layout.cat_item, parent, false);

//处理 RecyclerView 点击事件

final ViewHolder holder = new ViewHolder(view);

holder.cardView.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int position = holder.getAdapterPosition();

Cat cat = cats.get(position);

Intent intent = new Intent(context, CatActivity.class);

intent.putExtra(CatActivity.CAT_NAME, cat.getType());

intent.putExtra(CatActivity.CAT_IMAGE_ID, cat.getImgId());

context.startActivity(intent);

}

});

return holder;

}

...

}

运行效果:

a8e7adcc189d?utm_source=oschina-app

3 融合系统顶部状态栏

利用 fitsSystemWindows 属性我们可以将标题栏中的图片与系统顶部状态栏融合起来。

注意: 因为我们这里的 ImageView 嵌套的很深,所以必须把它以及它的祖先布局中的 fitsSystemWindows 都设置为 true,才能真正生效:

xmlns:material="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

>

android:id="@+id/app_bar_layout"

android:layout_width="match_parent"

android:layout_height="250dp"

android:fitsSystemWindows="true"

>

android:id="@+id/collapsing_toolbar_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

material:contentScrim="?attr/colorPrimary"

material:layout_scrollFlags="scroll|exitUntilCollapsed"

android:fitsSystemWindows="true"

>

android:id="@+id/cat_image_view"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="centerCrop"

material:layout_collapseMode="parallax"

android:fitsSystemWindows="true"

/>

...

...

接着把状态栏的颜色设置为透明,即

@android:color/transparent

因为这个属性从 API21 (Android 5.0 +)才出现,所以我们必须做差异化实现。

在 res 目录下新建一个 values-v21 目录,然后再新建一个 styles.xml:

@android:color/transparent

这样设计是因为只有 Android 5.0+ 才会去读取该文件。

我们还需要在 values/styles.xml 中新建一个不包含实际内容的 catActivityTheme,确保 Android 5.0 之前的版本不会抛错:

最后在 AndroidManifest.xml 中的 CatActivity 活动,设置刚刚定义的主题:

android:theme="@style/catActivityTheme"

>

运行效果:

a8e7adcc189d?utm_source=oschina-app

就算是现在的刘海屏,也可以适配的很好哦O(∩_∩)O~

这篇关于android 好看的标签栏,说说 Android 的 Material Design 设计(五)——可折叠式标题栏...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/386995

相关文章

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

CSS去除a标签的下划线的几种方法

《CSS去除a标签的下划线的几种方法》本文给大家分享在CSS中,去除a标签(超链接)的下划线的几种方法,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧... 在 css 中,去除a标签(超链接)的下划线主要有以下几种方法:使用text-decoration属性通用选择器设置:使用a标签选择器,将tex

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式