Android实现可折叠式标题栏
作者:路宇 发布时间:2022-11-05 14:55:05
本文实例为大家分享了Android实现可折叠式标题栏的具体代码,供大家参考,具体内容如下
先看效果图:
一、实现步骤:
1、布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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=".activity.FruitActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="250dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing"
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">
<ImageView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/head"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:id="@+id/nested_scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="35dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="4dp">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="我这里是一个卡片布局!" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#00000000"
android:src="@drawable/comment"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end"></com.google.android.material.floatingactionbutton.FloatingActionButton>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
接下来我们来分析这里面的控件和属性:
1、最外层的布局为 CoordinatorLayout
:相当于加强版的FrameLayout,在普通情况下的作用和FrameLayout基本一致。当然也会有其独特的作用,CoordinatorLayout可以监听其所有子控件的各种事件,然后自动帮我们做出最为合理的响应。
2、AppBarLayout:实际上是一个垂直方向的LinearLayout,在内部做了很多封装,并应用了一些Material Design的设计理念。
3、CollapsingToolbarLayout是作用于Toolbar基础之上的一个布局,CollapsingToolbarLayout可以让Toolbar的效果变得更加丰富。
4、app:layout_scrollFlags="scroll|exitUntilCollapsed"属性:srcoll表示CollapsingToolbarLayout会随着内容的滚动一起滚动,exitUntilCollapsed表示当CollapsingToolbarLayout随着滚动完成折叠之后就保留在界面上,不再移出屏幕。
5、app:contentScrim="?attr/colorPrimary"属性:用于指定在CollapsingToolbarLayout在趋于折叠状态以及折叠之后的背景色。
6、app:layout_collapseMode="pin"属性:用于指定在控件CollapsingToolbarLayout折叠过程中的折叠模式,pin表示在折叠过程中位置始终不变。
7、app:layout_collapseMode=“parallax” 属性:表示在折叠的过程中产生一定的错位偏移。
8、NestedScrollView控件:即有ScrollView控件使用滚动的方式来查看屏幕以外的数据,NestedScrollView在此基础之上还增加了嵌套响应滚动事件的功能。
9、app:layout_behavior="@string/appbar_scrolling_view_behavior"指定了一个布局行为
10、CardView:用于实现卡片式布局效果的重要控件,额外提供了圆角和阴影的效果。
11、app:cardCornerRadius属性:指定卡片圆角的弧度。
12、FloatingActionButton悬浮按钮
关于控件和属性就说这么多。
接下来就是实现java代码了,代码如下:
public class FruitActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsing;
private Toolbar toolbar;
private FloatingActionButton floating;
private TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruit);
collapsing = findViewById(R.id.collapsing);
toolbar = findViewById(R.id.toolbar);
floating = findViewById(R.id.floating);
tv_text = findViewById(R.id.tv_text);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
collapsing.setTitle("这是CollapsingToolbarLayout");
String text = "努力努力再努力";
tv_text.setText(generateText(text));
floating.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(FruitActivity.this, "您点击了悬浮按钮哦!", Toast.LENGTH_SHORT).show();
}
});
}
private String generateText(String text) {
StringBuilder stringBuilder = new StringBuilder("");
for (int i = 0; i < 500; i++) {
stringBuilder.append(text);
}
return stringBuilder.toString();
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
//Toolbar左上角默认有一个返回的箭头,含义是返回上一个活动
//这个按钮叫做HomeAsUp按钮,这个按钮的id永远都是android.R.id.home
case android.R.id.home:
finish();
break;
}
return true;
}
}
来源:https://blog.csdn.net/lu202032/article/details/120383111


猜你喜欢
- 1、系统信息:Windows10 64位2、环境搭建参考:https://www.jb51.net/article/185086.htm3、
- storm操作zookeeper的主要函数都定义在命名空间backtype.storm.cluster中(即cluster.clj文件中)。
- 根据约定,在使用java编程的时候应尽可能的使用现有的类库,当然你也可以自己编写一个排序的方法,或者框架,但是有几个人能写得比JDK里的还要
- 在gitee上创建springcloud仓库 application.yaml(https方式)server: por
- 现在,让我们找出“如何学习 Java 编程”的答案。通过承认您是初学者这一事实开始您的学习之旅很重要
- final 可以适用的范围:修饰类:使用这种修饰符的类无法被继承修饰函数:被修饰的不能被重写修饰属性:1.final修饰的成员变量是常量,值
- String和List<String>间相互转换public void test() {  
- 泛型约束的意思就是说:类的泛型,只能是where字句后面所写的接口或类。这么说好像也有点不大明白,举个例子。我有一个接口,如下:
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- Android 界面刷新 Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,
- 前言在实际开发当中,对于某些关键业务,我们通常需要记录该操作的内容,一个操作调一次记录方法,每次还得去收集参数等等,会造成大量代码重复。 我
- 这篇文章主要介绍了Java开发工具IntelliJ IDEA安装图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习
- 当只需要两个图像合并的时候,可以简单的使用gdi+,把两个图像画到一个画布上面实现合并bitmap.当需要将许多bitmap合并时,由于bi
- 1 前言为什么我们在使用SpringBoot框架开发Java Web应用需要引入大量的starter?例如,我们引入Redis就在Maven
- 本文实例为大家分享了Android仿今日头条订阅频道,供大家参考,具体内容如下源码:Android实现今日头条订阅频道布局文件<?xm
- 本篇是对安卓菜单使用编程方式实现,当然可以使用XML的方式完成同样的功能,基本Java和C#写法都是一致的,所以使用XML的方式在本篇中使用
- IntelliJ IDEA是广受Java开发者喜爱的工具,其商业版的价格十分昂贵,如下图:现在有机会免费获取IntelliJ IDEA的正版
- 本文实例为大家分享了C语言实现生日贺卡的具体代码,供大家参考,具体内容如下//********** 编译环境VC6.0 **********
- SpringBoot默认的页面映射路径(即模板文件存放的位置)为“classpath:/templates/*.html”。静态文件路径为“
- 由C#转入Java一段时间了,总结下个人认为的Java同C#语法之间的不同之处,有不同意见之处还望各位海涵 刚学Java时觉得语法同C#大致