Android实现可折叠式标题栏_第1页
Android实现可折叠式标题栏_第2页
Android实现可折叠式标题栏_第3页
Android实现可折叠式标题栏_第4页
Android实现可折叠式标题栏_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第Android实现可折叠式标题栏本文实例为大家分享了Android实现可折叠式标题栏的具体代码,供大家参考,具体内容如下

先看效果图:

一、实现步骤:

1、布局文件

xmlversion="1.0"encoding="utf-8"

androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns:android="/apk/res/android"

xmlns:app="/apk/res-auto"

xmlns:tools="/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,在内部做了很多封装,并应用了一些MaterialDesign的设计理念。

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代码了,代码如下:

publicclassFruitActivityextendsAppCompatActivity{

privateCollapsingToolbarLayoutcollapsing;

privateToolbartoolbar;

privateFloatingActionButtonfloating;

privateTextViewtv_text;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

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);

ActionBaractionBar=getSupportActionBar();

if(actionBar!=null){

actionBar.setDisplayHomeAsUpEnabled(true);

}

collapsing.setTitle("这是CollapsingToolbarLayout");

Stringtext="努力努力再努力";

tv_text.setText(generateText(text));

floating.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

Toast.makeText(FruitActivity.this,"您点击了悬浮按钮哦!",Toast.LENGTH_SHORT).show();

}

});

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论