1

我使用Xamarin和我的ViewPager有一些代碼的麻煩,不顯示我的意見。ViewPager的意見不顯示

這裏是我的代碼:

MainActivity:

public class MainActivity : FragmentActivity 
{ 
    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 

     SetContentView (Resource.Layout.activity_main); 

     var pager = FindViewById<ViewPager>(Resource.Id.viewPager); 
     pager.Adapter = new MyPagerAdapter(SupportFragmentManager); 
    } 
} 

MyPagerAdapter:

public class MyPagerAdapter : FragmentPagerAdapter 
{ 
    int count; 

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

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

     switch(position) 
     { 
     case 0: return FirstFragment.newInstance("FirstFragment, Instance 1"); 
     case 1: return SecondFragment.newInstance("SecondFragment, Instance 1"); 
     case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1"); 
     case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2"); 
     case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3"); 
     default: return ThirdFragment.newInstance("ThirdFragment, Default"); 
     }  
    } 

    public MyPagerAdapter (Android.Support.V4.App.FragmentManager fm) : base (fm) 
    { 
     count = 5; 
    }  
} 

FirstFragment:

public class FirstFragment : Android.Support.V4.App.Fragment 
{ 
    string text; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View v = inflater.Inflate(Resource.Layout.first_frag, container, false); 

     TextView tv = (TextView) v.FindViewById(Resource.Id.tvFragFirst); 
     tv.Text = Arguments.GetString("msg"); 

     return v; 
    } 

    public static FirstFragment newInstance(String text) 
    { 

     FirstFragment f = new FirstFragment(); 
     Bundle b = new Bundle(); 
     b.PutString("msg", text); 

     f.Arguments = (b); 

     return f; 
    } 
} 

SecondFragment:

public class SecondFragment : Android.Support.V4.App.Fragment 
{ 
    string text; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.Inflate(Resource.Layout.second_frag, container, false); 

     TextView tv = (TextView) v.FindViewById(Resource.Id.tvFragSecond); 
     tv.Text = Arguments.GetString("msg"); 

     return v; 
    } 

    public static SecondFragment newInstance(String text) 
    { 

     SecondFragment f = new SecondFragment(); 
     Bundle b = new Bundle(); 
     b.PutString("msg", text); 

     f.Arguments = (b); 

     return f; 
    } 
} 

ThirdFragment:

public class ThirdFragment : Android.Support.V4.App.Fragment 
{ 
    string text; 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.Inflate(Resource.Layout.third_frag, container, false); 

     TextView tv = (TextView) v.FindViewById(Resource.Id.tvFragThird); 
     tv.Text = Arguments.GetString("msg"); 

     return v; 
    } 

    public static ThirdFragment newInstance(String text) 
    { 

     ThirdFragment f = new ThirdFragment(); 
     Bundle b = new Bundle(); 
     b.PutString("msg", text); 

     f.Arguments = (b); 

     return f; 
    } 
} 

編輯

這裏是我的activity_main.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:background="#F3F3F4" 
android:orientation="vertical"> 
<android.support.v4.view.ViewPager 
    android:id="@+id/viewPager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="9dp" 
    android:paddingRight="9dp" /> 
</LinearLayout> 

當應用程序加載時,不會顯示任何內容。沒有錯誤發生。

我可以請一些幫助來獲得意見顯示?

預先感謝

EDIT2

下面是片段佈局:

first_frag.axml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_orange_dark"> 
    <TextView 
     android:id="@+id/tvFragFirst" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:textSize="26dp" 
     android:text="TextView" /> 
</RelativeLayout> 

second_frag.axml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_green_dark"> 
    <TextView 
     android:id="@+id/tvFragSecond" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:textSize="26dp" 
     android:text="TextView" /> 
</RelativeLayout> 

third_frag.axml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_red_light"> 
    <TextView 
     android:id="@+id/tvFragThird" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:textSize="26dp" 
     android:text="TextView" /> 
</RelativeLayout> 

而且,我有這個代碼從以下鏈接:提前 How to implement a ViewPager with different Fragments/Layouts

感謝

+0

你可以把你的佈局代碼與viewpager? – choper

+0

@choper:你能看看我的代碼嗎? – Garry

+0

顯示碎片的佈局。 –

回答

1

更改android:layout_height爲其他值,而不是0dp。由於身高爲0,你無法看到任何東西。

+0

我已更改佈局代碼,但未顯示任何視圖。我還應該做什麼? – Garry

+0

你是如何改變它的? – choper

+0

我刪除了android:layout_height值。 – Garry