2016-08-15 25 views
1

我想在Xamarin c#中構建一個android活動,用於一步一步的註冊和/或信息。我怎樣才能做這樣的事情:Xamarin一步一步的嚮導android的意見

enter image description here

誰能給我一個代碼示例或什麼?謝謝。

+0

附加信息:每個點是一個註冊頁面。 – Yaza

+0

對不起,不回覆。我沒有忘記你。 :)我會嘗試向你展示在接下來的幾天內要做什麼的樣本。 – amitairos

+0

嗨阿米特羅斯,我也試圖實現它,但我的視覺工作室正在瘋狂。所以我試圖解決這個問題。但是,謝謝你的回答和你的努力來幫助我。我會等你的樣品。 – Yaza

回答

3

基本上你需要使用一個名爲ViewPager的元素,並且每個頁面將是一個不同的Fragment。你可以使用this庫來幫助你。隨時提問和指導。

編輯 - 詳細說明:
兩個圖像添加到您的繪製文件夾 - 未選擇點之一,所選擇的點之一。 添加這兩個NuGet包(在解決方案資源管理器中右鍵單擊您的項目,單擊管理NuGet包並搜索):Xamarin.Android.Support.v4和Xamarin.Android.Support.v7.AppCompat。

然後你Main.axml佈局,把這個:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     app:popupTheme="@style/ThemeOverlay.AppCompat" 
     app:titleMarginTop="15dp"/> 
    <FrameLayout 
    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/viewPagerCountDots" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="10dp" 
       android:layout_gravity="bottom|center_horizontal" 
       android:orientation="horizontal"> 
     <ImageView 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_margin="10dp" 
     android:src="@drawable/ViewPagerDotSelected"/> 
     <ImageView 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_margin="10dp" 
     android:src="@drawable/ViewPagerDotUnselected"/> 
     <ImageView 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_margin="10dp" 
     android:src="@drawable/ViewPagerDotUnselected"/> 
    </LinearLayout> 
    </FrameLayout> 
</LinearLayout> 

你可以玩的寬度,高度,以及後來的保證金。

接下來,創建將包含您的不同頁面(或「步驟」)的新片段。在這個例子中,我創建了三個。

做到這一點三倍,同時改變文件及其內容的名稱:
右鍵單擊項目>>添加>>新產品>>片段,張貼下面的代碼爲例:

public class Fragment1 : Fragment 
{ 
    public override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
    } 

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     // Use this to return your custom view for this Fragment 
     var view = inflater.Inflate(Resource.Layout.Fragment1Layout, container, false); 

     //Here goes what you want to do within the fragment. 

     return view; 
    } 
} 

替換每個片段的類和佈局的名稱。

然後右鍵單擊您的佈局文件夾>>添加新項目>> Android佈局。
帖子下面的代碼爲例:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="FRAGMENT 1" 
    android:layout_gravity="center"/> 
</LinearLayout> 

做到這一點三倍,而更改名稱和內容。

現在,在你MainActivity.cs,張貼下面的代碼命名空間名稱後:

[Activity(Label = "ViewPagerIndicator", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/Theme.AppCompat.Light.DarkActionBar")] 
public class MainActivity : AppCompatActivity 
{ 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 
     var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); 
     SetSupportActionBar(toolbar); 
     SupportActionBar.Title = "ViewPager Indicator Dots"; 

     var pager = new ViewPagerAdapter(SupportFragmentManager); 
     var viewPager = FindViewById<ViewPager>(Resource.Id.viewPager); 
     viewPager.Adapter = pager; 
     viewPager.PageSelected += ViewPager_PageSelected; 
    } 

    private void ViewPager_PageSelected(object sender, ViewPager.PageSelectedEventArgs e) 
    { 
     var viewPagerDotsLayout = FindViewById<LinearLayout>(Resource.Id.viewPagerCountDots); 
     for (int i = 0; i < viewPagerDotsLayout.ChildCount; i++) 
     { 
      ImageView dotImage = (ImageView)viewPagerDotsLayout.GetChildAt(i); 
      if (i == e.Position) 
       dotImage.SetImageResource(Resource.Drawable.ViewPagerDotSelected); 
      else 
       dotImage.SetImageResource(Resource.Drawable.ViewPagerDotUnselected); 
     } 
    } 
} 


public class ViewPagerAdapter : FragmentStatePagerAdapter 
{ 
    int numberOfFragments = 3; 

    public ViewPagerAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm) 
    { 

    } 
    public override int Count 
    { 
     get 
     { 
      return numberOfFragments; 
     } 
    } 

    public override Android.Support.V4.App.Fragment GetItem(int position) 
    { 
     switch (position) 
     { 

      case 0: 
       return new Fragment1(); 
      case 1: 
       return new Fragment2(); 
      case 2: 
       return new Fragment3(); 
      default: return new Fragment1(); 
     } 
    } 
} 

運行它,看看它是否工作......

+1

是的,這是完美的工作。感謝您一步一步的解釋。完全清楚該怎麼做。非常感謝你的努力。 – Yaza

+0

@Yaza太棒了!別客氣! – amitairos