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

相关文章

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

基于Java实现回调监听工具类

《基于Java实现回调监听工具类》这篇文章主要为大家详细介绍了如何基于Java实现一个回调监听工具类,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录监听接口类 Listenable实际用法打印结果首先,会用到 函数式接口 Consumer, 通过这个可以解耦回调方法,下面先写一个

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Qt中QGroupBox控件的实现

《Qt中QGroupBox控件的实现》QGroupBox是Qt框架中一个非常有用的控件,它主要用于组织和管理一组相关的控件,本文主要介绍了Qt中QGroupBox控件的实现,具有一定的参考价值,感兴趣... 目录引言一、基本属性二、常用方法2.1 构造函数 2.2 设置标题2.3 设置复选框模式2.4 是否

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法

《springboot整合阿里云百炼DeepSeek实现sse流式打印的操作方法》:本文主要介绍springboot整合阿里云百炼DeepSeek实现sse流式打印,本文给大家介绍的非常详细,对大... 目录1.开通阿里云百炼,获取到key2.新建SpringBoot项目3.工具类4.启动类5.测试类6.测

pytorch自动求梯度autograd的实现

《pytorch自动求梯度autograd的实现》autograd是一个自动微分引擎,它可以自动计算张量的梯度,本文主要介绍了pytorch自动求梯度autograd的实现,具有一定的参考价值,感兴趣... autograd是pytorch构建神经网络的核心。在 PyTorch 中,结合以下代码例子,当你

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient