我正在使用Xamarin,并且我的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 :(得分:1)
将android:layout_height
更改为其他值,而不是0dp。当高度为0时,你无法看到任何东西。