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协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Mysql中设计数据表的过程解析

《Mysql中设计数据表的过程解析》数据库约束通过NOTNULL、UNIQUE、DEFAULT、主键和外键等规则保障数据完整性,自动校验数据,减少人工错误,提升数据一致性和业务逻辑严谨性,本文介绍My... 目录1.引言2.NOT NULL——制定某列不可以存储NULL值2.UNIQUE——保证某一列的每一

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局