android导航页(欢迎页实现十分简单)

2023-11-01 11:20

本文主要是介绍android导航页(欢迎页实现十分简单),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 首先要弄明白原理,大致了解一下该流程


    1、创建一个类继承自SimplePagerFragment,实现对导航页面的页面循环的设置、背景颜色的设置以及页面监听事件的设置
    2、创建类定义每一个的页面(有多少页就创建多少个页面,当然布局文件是肯定需要的不再赘叙)
    3、在Activity中启动
  • 创建创建一个类继承自SimplePagerFragment代码如下:
        
    1. package com.cleveroad.slidingtutorial.sample;
    2. import android.graphics.Color;
    3. import android.support.annotation.ColorInt;
    4. import android.support.v4.content.ContextCompat;
    5. import android.view.View;
    6. import android.widget.Toast;
    7. import com.cleveroad.slidingtutorial.PageFragment;
    8. import com.cleveroad.slidingtutorial.SimplePagerFragment;
    9. //此处继承自SimplePagerFragment查看库文件代码发现SimplePagerFragment继承自CustomPresentationPagerFragment
    10. public class CustomPresentationPagerFragment extends SimplePagerFragment {
    11. //获取页面个数
    12. @Override
    13. protected int getPagesCount() {
    14. return 6;
    15. }
    16. //注意此处是背景上所添加的图案的循环出现的次数,运行安装后可以发现第一个页面(position=0)的图案和第四个(position=3)是相同的就是在此处设置的
    17. @Override
    18. protected PageFragment getPage(int position) {
    19. position %= 3;
    20. if (position == 0)
    21. return new FirstCustomPageFragment();
    22. if (position == 1)
    23. return new SecondCustomPageFragment();
    24. if (position == 2)
    25. return new ThirdCustomPageFragment();
    26. throw new IllegalArgumentException("Unknown position: " + position);
    27. }
    28. //设置背景颜色,现在越来越多的软件背景都是以颜色代替呈现一种扁平化的感觉,也可以自定义图片
    29. @ColorInt
    30. @Override
    31. protected int getPageColor(int position) {
    32. if (position == 0)
    33. return ContextCompat.getColor(getContext(), android.R.color.holo_orange_dark);
    34. if (position == 1)
    35. return ContextCompat.getColor(getContext(), android.R.color.holo_green_dark);
    36. if (position == 2)
    37. return ContextCompat.getColor(getContext(), android.R.color.holo_blue_dark);
    38. if (position == 3)
    39. return ContextCompat.getColor(getContext(), android.R.color.holo_red_dark);
    40. if (position == 4)
    41. return ContextCompat.getColor(getContext(), android.R.color.holo_purple);
    42. if (position == 5)
    43. return ContextCompat.getColor(getContext(), android.R.color.darker_gray);
    44. return Color.TRANSPARENT;
    45. }
    46. //是否循环滑动(true一直循环滑动不进入程序,false相反)
    47. @Override
    48. protected boolean isInfiniteScrollEnabled() {
    49. return true;
    50. }
    51. //监听事件(页面上左下角Skip的点击事件---布局代码都在库文件中不可修改但是如果需要自己定义点击图标以及事件可以继承其抽象类的抽象方法去实现
    52. @Override
    53. protected boolean onSkipButtonClicked(View skipButton) {
    54. Toast.makeText(getContext(), "Skip button clicked", Toast.LENGTH_SHORT).show();
    55. return true;
    56. }
    57. }
  • 设置单个页面(有多少个就设置多少个是不是很简单!):
        
    1. public class FirstCustomPageFragment extends PageFragment {
    2. //获取布局文件的ID
    3. @Override
    4. protected int getLayoutResId() {
    5. return R.layout.fragment_page_first;
    6. }
    7. /*为界面上的各个元素设置移动因素,包括方向和系数。一个TransformItem就是一个界面元素,其中它的第一个参数是界面元素对应的id,第二个参数是是否反向,true表示要,false表示不,第三个参数是移动系数。系数越大移动越慢,为一个界面上的不同元素设置不同的方向和系数,就能形成视差效果。**/
    8. @Override
    9. protected TransformItem[] provideTransformItems() {
    10. return new TransformItem[]{
    11. new TransformItem(R.id.ivFirstImage, true, 20),
    12. new TransformItem(R.id.ivSecondImage, false, 6),
    13. new TransformItem(R.id.ivThirdImage, true, 8),
    14. new TransformItem(R.id.ivFourthImage, false, 10),
    15. new TransformItem(R.id.ivFifthImage, false, 3),
    16. new TransformItem(R.id.ivSixthImage, false, 9),
    17. new TransformItem(R.id.ivSeventhImage, false, 14),
    18. new TransformItem(R.id.ivEighthImage, false, 7)
    19. };
    20. }
    21. }
  • 相对应的布局文件(此处使用的是百分比布局,如果要使用该形式的布局是需要导入百分比布局的库文件的否则编译不通过:'com.android.support:percent:22.2.0'):
        
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <android.support.percent.PercentRelativeLayout
    3. android:id="@+id/rootFirstPage"
    4. xmlns:android="http://schemas.android.com/apk/res/android"
    5. xmlns:app="http://schemas.android.com/apk/res-auto"
    6. xmlns:tools="http://schemas.android.com/tools"
    7. android:layout_width="match_parent"
    8. android:layout_height="match_parent"
    9. tools:ignore="ContentDescription"
    10. tools:background="@android:color/holo_orange_dark">
    11. <ImageView
    12. android:id="@+id/ivFirstImage"
    13. android:layout_width="wrap_content"
    14. android:layout_height="wrap_content"
    15. android:layout_centerInParent="true"
    16. android:src="@mipmap/s_0_1"
    17. app:layout_heightPercent="35%"
    18. app:layout_widthPercent="50%"/>
    19. <ImageView
    20. android:id="@+id/ivSecondImage"
    21. android:layout_width="wrap_content"
    22. android:layout_height="wrap_content"
    23. android:layout_alignParentEnd="true"
    24. android:layout_alignParentRight="true"
    25. android:src="@mipmap/s_0_2"
    26. app:layout_heightPercent="10%"
    27. app:layout_marginRightPercent="12%"
    28. app:layout_marginTopPercent="27%"
    29. app:layout_widthPercent="12%"/>
    30. <ImageView
    31. android:id="@+id/ivThirdImage"
    32. android:layout_width="wrap_content"
    33. android:layout_height="wrap_content"
    34. android:src="@mipmap/s_0_3"
    35. app:layout_heightPercent="25%"
    36. app:layout_marginLeftPercent="14%"
    37. app:layout_marginTopPercent="49%"
    38. app:layout_widthPercent="30%"/>
    39. <ImageView
    40. android:id="@+id/ivFourthImage"
    41. android:layout_width="wrap_content"
    42. android:layout_height="wrap_content"
    43. android:src="@mipmap/s_0_4"
    44. app:layout_heightPercent="15%"
    45. app:layout_marginLeftPercent="14%"
    46. app:layout_marginTopPercent="39%"
    47. app:layout_widthPercent="20%"/>
    48. <ImageView
    49. android:id="@+id/ivFifthImage"
    50. android:layout_width="wrap_content"
    51. android:layout_height="wrap_content"
    52. android:layout_centerHorizontal="true"
    53. android:src="@mipmap/s_0_5"
    54. app:layout_heightPercent="15%"
    55. app:layout_marginTopPercent="22%"
    56. app:layout_widthPercent="45%"/>
    57. <ImageView
    58. android:id="@+id/ivSixthImage"
    59. android:layout_width="wrap_content"
    60. android:layout_height="wrap_content"
    61. android:src="@mipmap/s_0_6"
    62. app:layout_heightPercent="6%"
    63. app:layout_marginLeftPercent="4%"
    64. app:layout_marginTopPercent="26%"
    65. app:layout_widthPercent="6%"/>
    66. <ImageView
    67. android:id="@+id/ivSeventhImage"
    68. android:layout_width="wrap_content"
    69. android:layout_height="wrap_content"
    70. android:src="@mipmap/s_0_7"
    71. app:layout_heightPercent="8%"
    72. app:layout_marginLeftPercent="14%"
    73. app:layout_marginTopPercent="25%"
    74. app:layout_widthPercent="9%"/>
    75. <ImageView
    76. android:id="@+id/ivEighthImage"
    77. android:layout_width="wrap_content"
    78. android:layout_height="wrap_content"
    79. android:src="@mipmap/s_0_8"
    80. app:layout_heightPercent="6%"
    81. app:layout_marginLeftPercent="77%"
    82. app:layout_marginTopPercent="38%"
    83. app:layout_widthPercent="8%"/>
    84. <TextView
    85. android:layout_width="wrap_content"
    86. android:layout_height="wrap_content"
    87. android:layout_alignParentBottom="true"
    88. android:layout_centerHorizontal="true"
    89. android:gravity="center"
    90. android:text="@string/text_web_ceo"
    91. android:textColor="@android:color/white"
    92. android:textSize="@dimen/text_size_large"
    93. app:layout_heightPercent="15%"
    94. app:layout_marginBottomPercent="11%"
    95. app:layout_widthPercent="45%"/>
    96. </android.support.percent.PercentRelativeLayout>
    此时导航页面是无限循环的如果需要判断用户是否是第一次启动该软件用savedInstanceState判断一下,MainActivity 中代码较少也简单主要是用了一个replaceTutorialFragment()方法。
  • 具体代码下载地址:https://github.com/Cleveroad/SlidingTutorial-Android.git
  • 如果下载总是中间停止可以用SVN下载无需用户名及密码
  • 如转载请标明转载地址,写的不好尽情谅解!
  • 如有问题可以加我qq:2915859312联系!

这篇关于android导航页(欢迎页实现十分简单)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

hdu2289(简单二分)

虽说是简单二分,但是我还是wa死了  题意:已知圆台的体积,求高度 首先要知道圆台体积怎么求:设上下底的半径分别为r1,r2,高为h,V = PI*(r1*r1+r1*r2+r2*r2)*h/3 然后以h进行二分 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#includ

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影