我正在处理一个在Layout中包含PageViewer的应用程序。
但如果我启动它,则看不到PageViewer。 (看起来很可能layout_height = 0)
我从来没有在其他布局中使用过PageViewer,所以也许它不是为某些东西而设计的?
布局文件(缩写,activity_tricks.xml):
<linearLayout>
<TableRow>
....
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrapt_content">
<android.support.v4.view.ViewPager
android:id="@+id/Activity_Trick_viewPager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="6dip" />
</TableRow>
<TableRow>
.
.
.
Trick_Fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/trick_fragment_textView_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
的活动:
public class TrickActivity extends FragmentActivity {
private ViewPager mViewPager;
private TrickActivityPageAdapter mPageAdapter;
public static final int FRAMES = 3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tricks);
mPageAdapter= new TrickActivityPageAdapter(getSupportFragmentManager(),frames);
mViewPager=(ViewPager)findViewById(R.id.Activity_Trick_viewPager);
mViewPager.setAdapter(mPageAdapter);
//... do other stuff.. //
}
}
public class TrickActivityPageAdapter extends FragmentPagerAdapter {
private int frames;
public TrickActivityPageAdapter(FragmentManager fm, int frames) {
super(fm);
this.frames = frames;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TrickFragment.init("F 1");
case 1:
return TrickFragment
.init("F 2");
case 2:
return TrickFragment.init("f3");
default:
return TrickFragment.init("default");
}
}
@Override
public int getCount() {
return frames;
}
}
public class TrickFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
public static Fragment init(String string) {
TrickFragment f = new TrickFragment();
Bundle bdl = new Bundle();
bdl.putString(EXTRA_MESSAGE, string);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.trick_fragment_layout, container, false);
TextView tv = (TextView)v.findViewById(R.id.trick_fragment_textView_1);
tv.setText(message);
return v;
}