android渐变Toolbar的实现
来源:互联网 发布:圣安德鲁斯大学cs知乎 编辑:程序博客网 时间:2024/05/16 02:31
toolbar渐变效果很常见,实现方法也多,先看效果图
采用最简单的实现方式。直接看code
第一部分:布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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" android:fitsSystemWindows="true" tools:context="com.example.minwenping.toolbarjianbiandemo.MainActivity"> <com.example.minwenping.toolbarjianbiandemo.XScrollView android:id="@+id/scrollView" android:clipToPadding="false" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?android:actionBarSize"> <android.support.v7.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="4dp" android:text="按钮button1" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="4dp" android:text="按钮button2" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="4dp" android:text="按钮button3" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="4dp" android:text="按钮button4" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="4dp" android:text="按钮button5" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="4dp" android:text="按钮button6" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="4dp" android:text="按钮button7" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="4dp" android:text="按钮button1" /> <Button android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="4dp" android:text="按钮button1" /> </android.support.v7.widget.LinearLayoutCompat> </com.example.minwenping.toolbarjianbiandemo.XScrollView> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:background="@color/colorAccent" android:titleTextColor="@color/colorPrimary" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" app:title="Hello World!粉红色的toolbar" /></RelativeLayout>
第二部分是自定义scrollview的code,因为scrollview的滑动监听不兼容低版本,所以继承重写
public class XScrollView extends ScrollView { int height; public void setAlphaChangeListener(AlphaChangeListener alphaChangeListener) { this.alphaChangeListener = alphaChangeListener; } AlphaChangeListener alphaChangeListener; public XScrollView(Context context) { this(context,null); } public XScrollView(Context context, AttributeSet attrs) { this(context, attrs,0); } public XScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); height=getResources().getDisplayMetrics().heightPixels; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (alphaChangeListener!=null) { if (t<=height/4){ float a=1-t*4.0f/height; alphaChangeListener.alphaChanging(a); } } }}
第三部分code,外部使用的接口回调
public interface AlphaChangeListener { abstract void alphaChanging(float alpha);}
第四部分code,activity中的使用
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolBar = ((Toolbar) findViewById(R.id.toolbar)); scrollview = ((XScrollView) findViewById(R.id.scrollView)); setSupportActionBar(toolBar); scrollview.setSmoothScrollingEnabled(true); scrollview.setAlphaChangeListener(this); } @Override public void alphaChanging(float alpha) { toolBar.setAlpha(alpha); }
阅读全文
0 0
- android渐变Toolbar的实现
- Android炫酷的Toolbar+Bottom+Fab悬浮按钮显示、隐藏、渐变的各种实现姿势
- toolbar的颜色渐变
- Android CoordinatorLayout+AppBarLayout实现toolbar背景渐变透明
- CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+Toolbar实现渐变透明的状态栏
- Android中Toolbar的实现
- Android ToolBar+DrawerLayout的实现
- Android渐变标题栏的实现
- 自定义ScrollView实现Toolbar(标题栏)渐变
- Android 监听手指滑动,Toolbar颜色渐变
- 自定义CoordinatorLayout的Behavior(2):实现淘宝和QQ ToolBar透明渐变效果
- 自定义CoordinatorLayout的Behavior(2):实现淘宝和QQ ToolBar透明渐变效果
- Android之可折叠Toolbar的实现
- Android渐变色圆角按钮的实现
- android渐变动画的两种实现
- Android的TitleBar实现透明度渐变效果
- Android底部bottom的渐变实现
- Android状态栏渐变色的实现
- Android之Button按钮点击事件的四种方式
- 1337: 平方和与立方和
- easyui需引入的文件
- 查找算法——折半查找
- Unity 游戏框架搭建 (十三) 无需继承的单例的模板
- android渐变Toolbar的实现
- 用json模块dumps函数处理的两个小应用
- Android开发选取相册多张图片
- AsyncTask与线程池
- Unity编辑器Unity Editor基础(一)
- 简单介绍 Kotlin for Android
- 动静态库的制作与调用
- 《挑战程序设计竞赛》阅读笔记一 之 ALDS1_1_A Insertion Sort
- 【bzoj1699】[Usaco2007 Jan]Balanced Lineup排队