问题描述
我使用Xamarin和我有关于一些code麻烦,我ViewPager不显示我的看法。
I am using Xamarin and am having some code trouble in relation to my ViewPager not displaying my Views.
下面是我的code:
MainActivity:
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:
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:
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:
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:
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
布局code:
Here is my activity_main.axml
layout code:
<?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>
当应用程序加载时,不会显示任何内容。没有错误存在的。
When the application loads up, nothing is displayed. No errors are occuring.
我可以请有一定的帮助才能显示的看法?
Can I please have some help to get the views displaying?
在此先感谢
EDIT2
下面是片段的布局:
first_frag.axml
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
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
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>
另外,我有从以下链接此code:
How实施ViewPager不同片段/布局
在此先感谢
推荐答案
更改的android:layout_height
为其他值,而不是0dp。由于高度为0,你无法看到任何东西。
Change the android:layout_height
to some other value instead 0dp. As the height is 0 you was not able to see anything.
这篇关于ViewPager意见不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!