Xamarin.Android之引导页的简单制作
0x01 前言
对于现在大部分的APP,第一次打开刚安装或更新安装的APP都会有几个引导界面,通常这几个引导页是告诉用户
APP有些什么功能或者修改了什么bug、新增了什么功能等等等。
0x02 页面布局编写
新建一个Android项目
添加几个简单的布局页面。
首先是添加个启动页面,splash.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/ll"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24.0dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:padding="15.0dip"
android:src="@drawable/dot" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:padding="15.0dip"
android:src="@drawable/dot" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:clickable="true"
android:padding="15.0dip"
android:src="@drawable/dot" />
</LinearLayout>
</RelativeLayout>
引导页,用了一个ViewPager,下面的线性布局是用来存放的三个点就是当前所在的引导页面。
用到的ImageView有个src指向drawable下面的dot.xml。内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="@drawable/dark_dot" />
<item android:state_enabled="false" android:drawable="@drawable/white_dot" />
</selector>
然后是3个引导页的具体内容。
guide_first.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="guide---first"
android:textSize="30sp" />
</LinearLayout>
guide_second.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="guide--second"
android:textSize="30sp" />
</LinearLayout>
guide_third.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="250dp"
android:gravity="center"
android:text="guide--third"
android:textSize="30sp" />
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="begin now"
android:layout_gravity="center"
android:layout_marginBottom="55dp" />
</LinearLayout>
这里没有用图片来展示,就简单的用了文字,没有设计师设计,so....随意一点。
最后是Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_vertical"
android:text="the main page"
android:textSize="30sp" />
</LinearLayout>
0x03 Activity的编写
首先SplashActivity
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Widget;
namespace GuideDemo
{
[Activity(Label = "GuideDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.splash);
//version's infomation
var version = PackageManager.GetPackageInfo(this.PackageName, PackageInfoFlags.MatchAll).VersionName;
var tvVersion = FindViewById<TextView>(Resource.Id.tv_version);
tvVersion.Text = "Version " + version;
//get infomation from shared preferences
var sp = GetSharedPreferences("app_setting", FileCreationMode.Private);
new Handler().PostDelayed(() =>
{
Intent intent;
//first or not
if (sp.GetString("version", "") != version)
{
intent = new Intent(this, typeof(GuideActivity));
}
else
{
intent = new Intent(this, typeof(MainActivity));
}
StartActivity(intent);
this.Finish();
}, );
}
}
}
把是否是第一次开启信息存放到sharepreferences,我这里主要是通过版本号来控制。
然后是GuideActivity
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Support.V4.View;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using static Android.Support.V4.View.ViewPager;
namespace GuideDemo
{
[Activity(Label = "GuideActivity")]
public class GuideActivity : Activity
{
private ViewPager viewPager; private List<View> views; private View view1, view2, view3; private Button btnStart; private ImageView[] dots; private int currentIndex;
private LinearLayout ll;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_guide);
viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
//the layout
LayoutInflater mLi = LayoutInflater.From(this);
view1 = mLi.Inflate(Resource.Layout.guide_first, null);
view2 = mLi.Inflate(Resource.Layout.guide_second, null);
view3 = mLi.Inflate(Resource.Layout.guide_third, null);
views = new List<View>();
views.Add(view1);
views.Add(view2);
views.Add(view3); //the adapter
viewPager.Adapter = new ViewPagerAdapter(views);
//page selected
viewPager.PageSelected += PageSelected;
ll = FindViewById<LinearLayout>(Resource.Id.ll);
//the dot infomation
dots = new ImageView[];
for (int i = ; i < views.Count; i++)
{
dots[i] = (ImageView)ll.GetChildAt(i);
dots[i].Enabled = false;
}
dots[].Enabled = true;
//the button
btnStart = view3.FindViewById<Button>(Resource.Id.startBtn);
btnStart.Click += Start;
}
/// <summary>
/// page selected
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PageSelected(object sender, PageSelectedEventArgs e)
{
ll = FindViewById<LinearLayout>(Resource.Id.ll);
for (int i = ; i < views.Count; i++)
{
dots[i] = (ImageView)ll.GetChildAt(i);
dots[i].Enabled = false;
}
dots[e.Position].Enabled = true;
}
/// <summary>
/// start the main page
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Start(object sender, EventArgs e)
{
//get infomation from shared preferences
var sp = GetSharedPreferences("app_setting", FileCreationMode.Private);
//the editor
var editor = sp.Edit();
//update
editor.PutString("version", PackageManager.GetPackageInfo(this.PackageName, PackageInfoFlags.MatchAll).VersionName).Commit() ;
StartActivity(typeof(MainActivity));
this.Finish();
}
}
}
主要是ViewPager处理、页面切换点的处理以及begin now 按钮的点击事件。
其中有个ViewPagerAdapter要自己实现
using Android.Support.V4.View;
using Android.Views;
using System.Collections.Generic;
namespace GuideDemo
{
internal class ViewPagerAdapter : PagerAdapter
{
private List<View> views;
public ViewPagerAdapter(List<View> views)
{
this.views = views;
}
public override int Count
{
get
{
if (views != null)
{
return views.Count;
}
else
{
return ;
}
}
}
public override bool IsViewFromObject(View view, Java.Lang.Object objectValue)
{
return view== objectValue;
}
public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object objectValue)
{
container.RemoveView(views[position]);
}
public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
{
container.AddView(views[position], );
return views[position];
}
}
}
最后是MainActivity
using Android.App;
using Android.OS;
namespace GuideDemo
{
[Activity(Label = "GuideDemo")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle); SetContentView(Resource.Layout.Main);
}
}
}
到这里就OK了,下面就来看看效果。
0x04 效果图

Xamarin.Android之引导页的简单制作的更多相关文章
- [置顶]
Android App引导页这些坑你自己犯过吗?
场景:测试机:华为荣耀6x 今天我自己掉入一个很蠢蠢的坑,一个引导页搞了20多分钟,不管我怎么测试用真机还是模拟器都无法运行,但是我写的demo完全没问题,好无语,我都怀疑我是不是搞android,我 ...
- Android App引导页这些坑你自己犯过吗?
场景:測试机:华为荣耀6x 今天我自己掉入一个非常蠢蠢的坑,一个引导页搞了20多分钟.无论我怎么測试用真机还是模拟器都无法执行,可是我写的demo全然没问题,好无语,我都怀疑我是不是搞android, ...
- Android 高级UI设计笔记22:Android 指示引导页(带圆点)
1. 引导页: 我们在安装某个软件首次运行时,大部分都会有一个引导页的提示,介绍软件新功能的加入或者使用说明等,支持滑动且下面会有几个圆点,显示共有多少页和当前图片的位置,类似如下效果: 2. 引导页 ...
- Android:启动引导页实现
前言 基本上现在所有的应用都会有一个欢迎界面,在欢迎界面对应用做一个整体的介绍,然后在跳入到主界面,这次要说的这个引导页就是带翻页的引导页.效果如下所示
- [iOS] App引导页的简单实现 (Swift 2)
转载请注明出处:http://www.jianshu.com/p/024dd2d6e6e6# 已更新至 Xcode7.2.Swift2.1 在第一次打开App或者App更新后通常用引导页来展示产品特性 ...
- Xamarin.Android 引导页
http://blog.csdn.net/qq1326702940/article/details/78665588 https://www.cnblogs.com/catcher1994/p/555 ...
- Android高级控件(四)——VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷
Android高级控件(四)--VideoView 实现引导页播放视频欢迎效果,超级简单却十分的炫酷 是不是感觉QQ空间什么的每次新版本更新那炫炫的引导页就特别的激动,哈哈,其实他实现起来真的很简单很 ...
- Xamarin.Android和UWP之MVVM的简单使用(二)
0x01 前言 前面一篇,Xamarin.Android和UWP之MVVM的简单使用(一),主要讲了MvvmLight的简单使用 这篇主要讲讲MvvmCross的简单使用,例子的话,还是和上篇的一样. ...
- Android引导页设计
大家在安装好一个应用后,第一次打开时往往会出现一个使用引导页,形式一般为三.四张图片,随着我们的滑动进行切换,在最后一页会有一个进入应用的按钮,我们通过点击这个按钮可以进入应用,其实这其中没有太多的复 ...
随机推荐
- Android ANR 分析解决方法
一:什么是ANR ANR:Application Not Responding,即应用无响应 二:ANR的类型 ANR一般有三种类型: 1. KeyDispatchTimeout(5 seconds) ...
- C#_基础:排序算法
//希尔排序 static int[] ShellSort(int[] array) { if (array != null) { int[] list = { 9, 5, 3, 2, 1 }; fo ...
- 利用gulp解决前后端分离的header/footer引入问题
在我们进行前后端完全分离的时候,有一个问题一直是挺头疼的,那就是公共header和footer的引入.在传统利用后端渲染的情况下,我们可以把header.footer写成两个单独的模板,然后用后端语言 ...
- Functional Programming without Lambda - Part 1 Functional Composition
Functions in Java Prior to the introduction of Lambda Expressions feature in version 8, Java had lon ...
- fir.im Weekly - 暖心的 iOS 持续集成,你值得拥有
一则利好消息,flow.ci 支持 iOS 项目持续集成,想试试的伙伴去 Gitter群 问问.首批尝鲜用户@阿米amoy 已经用 flow.ci 实现了基本的 iOS 持续集成,并详细记录整个 Bu ...
- Nginx内置变量
$args #请求中的参数值 $query_string #同 $args $arg_NAME #GET请求中NAME的值 $is_args #如果请求中有参数,值为"?",否则为 ...
- Kafka随笔一
一.KafKa所涉及到的名词概念: 1. Topic:用于划分Message的逻辑概念,一个Topic可以分布在多个Broker上. 2. Partition:是Kafka中横向扩展和一切 ...
- jquery-懒加载插件
在Web应用程序中,系统的瓶颈常在于系统的响应速度.如果系统响应速度过慢,用户就会出现埋怨情绪, 系统的价值也因此会大打折扣.因此,提高系统响应速度,是非常重要的. 从此可知,再好的网站,再炫的网站, ...
- 对Big Table进行全表更新,导致 Replication 同步数据的过程十分缓慢
在Publisher database中更新一个big table,数据行数是3.4亿多.由于没有更新 clustered Index key,因此,只产生了3.4亿多个Update Commands ...
- VMware Workstation and Hyper-V are not compatible. 解决方案
VMware 和 Hyper-V 不能共存问题报错如下:VMware Workstation and Hyper-V are notcompatible. Remove the Hyper-V rol ...