Xamarin Android 欢迎页

1.前言

现在市面上的APP大多数在首次安装的时候都有欢迎页,比如说还有轮播图之类的广告,所以欢迎页在APP的应用是非常广泛的,其实实现也是比较简单的,主要是用到ViewPager.

2.页面布局编写

ViewPager.xaml 这是外层包括图片的ViewPager,其实欢迎页的简单实现就是外层一个ViewPager,然后放布局界面进去。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
        android:id="@+id/welcomeViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000" />
LinearLayout>

布局界面WelcomePage.xaml,这里就是简单地放一张图片。


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/welcome01">
    <Button
        android:id="@+id/welcomeBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="35dp"
        android:background="@drawable/welcome_btn" />
RelativeLayout>

读者可以按照自己的要求去编写布局界面,这里只是讲解简单的实现。

3.Activity的编写

ViewPagerAdapter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.View;
using Java.Lang;

namespace Welcome
{
    public class ViewPagerAdapter : PagerAdapter
    {
        private static string SHAREDPREFERENCES_NAME = "first_pref";
        // 界面列表 
        private List views;
        private Activity activity;

        public ViewPagerAdapter(List views, Activity activity)
        {
            this.views = views;
            this.activity = activity;
        }

        /// 
        /// 获得当前界面数  
        /// 
        public override int Count
        {
            get
            {
                {
                    return views.Count();
                }
            }
        }

        // 初始化arg1位置的界面  
        public override Java.Lang.Object InstantiateItem(ViewGroup arg0, int arg1)
        {
            arg0.AddView(views[arg1]);
            if (arg1 == views.Count - 1)
            {
                Button welcomebtn = arg0.FindViewById

WelcomeActivity.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.View;

namespace Welcome
{
    [Activity(Label = "WelcomeActivity")]
    public class WelcomeActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            RequestWindowFeature(WindowFeatures.NoTitle);
            Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);  //设置全屏显示
            SetContentView(Resource.Layout.WelcomePage);
            ViewPager viewPager = FindViewById(Resource.Id.welcomeViewPager);
            //动态加载三个View
            var li = LayoutInflater.From(this);
            View v1 = li.Inflate(Resource.Layout.WelcomePage1, null);
            View v2 = li.Inflate(Resource.Layout.WelcomePage2, null);
            View v3 = li.Inflate(Resource.Layout.WelcomePage3, null);
            View v4 = li.Inflate(Resource.Layout.WelcomePage4, null);
            List listview = new List();
            listview.Add(v1);
            listview.Add(v2);
            listview.Add(v3);
            listview.Add(v4);
            ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(listview, this);
            viewPager.Adapter = viewPagerAdapter;
        }
    }
}

到这里就简单地实现了广告图。

你可能感兴趣的:(Xamarin,Android)