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

相关文章

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

Python实现文件下载、Cookie以及重定向的方法代码

《Python实现文件下载、Cookie以及重定向的方法代码》本文主要介绍了如何使用Python的requests模块进行网络请求操作,涵盖了从文件下载、Cookie处理到重定向与历史请求等多个方面,... 目录前言一、下载网络文件(一)基本步骤(二)分段下载大文件(三)常见问题二、requests模块处理

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

PyCharm接入DeepSeek实现AI编程的操作流程

《PyCharm接入DeepSeek实现AI编程的操作流程》DeepSeek是一家专注于人工智能技术研发的公司,致力于开发高性能、低成本的AI模型,接下来,我们把DeepSeek接入到PyCharm中... 目录引言效果演示创建API key在PyCharm中下载Continue插件配置Continue引言

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创