这是背景信息。
使用此代码编辑带有一些自定义属性(自定义字体、图像等)的文本和图像
//Font Loader
Typeface ralewayFont = Typeface.createFromAsset(getAssets(), "fonts/raleway.ttf");
Typeface abeatFont = Typeface.createFromAsset(getAssets(), "fonts/abeat.otf");
TextView hiText = (TextView) findViewById(R.id.hi);
TextView welcomeText = (TextView) findViewById(R.id.welcome);
TextView chancechatText = (TextView) findViewById(R.id.chancechat);
hiText.setTypeface(ralewayFont);
welcomeText.setTypeface(ralewayFont);
chancechatText.setTypeface(abeatFont);
那是一小段摘录。它被放置在主活动的 onCreate 方法下。
现在我已经实现了这样的片段。我在哪里实现代码。(我已经尝试过旧代码和 WelcomePage 方法。这里是新代码。有什么想法吗?还要注意我已将我的 editTexts 从 activity_main.xml 移动到 fragment_main_dummy.xml
public class MainActivity extends FragmentActivity {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new Fragment();
switch (position) {
case 0:
return fragment = new WelcomePage();
case 1:
return fragment = new Facebook();
default:
break;
}
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
public static class WelcomePage extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_main_dummy, container, false);
return rootView;
}
}
public static class Facebook extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_main_two, container, false);
return rootView;
}
}
}