0
繼承人背景信息。無法以編程方式實現片段後編輯文本
使用此代碼編輯文本和圖像的一些自定義的屬性(自定義字體,圖像等)
//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方法。在這裏,新的代碼。任何想法?另外要注意,我不得不從activity_main.xml中移動我editTexts到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;
}
}
}
由於WelcomePage是靜態類,因此無法獲取Assets。有任何想法嗎?我相信你所假設的是正確的。 –
我通過一些更改更新了我的答案。我不確定這是否是最好的解決方案,但它應該起作用。基本上,因爲你的WelcomePage類沒有擴展Activity並且它是靜態的,所以你不能直接訪問應用程序上下文。因此,我們需要在主活動中保存對其的引用,然後在實例化WelcomePage片段時將其傳遞給它。 – btse
查看onActivityCreated(Bundle savedInstanceState){ 所以這行在Eclipse中引發錯誤。它說「返回類型與Fragment.onActivityCreated(Bundle)不兼容」 任何想法?感謝迄今爲止的幫助,我很感激。 –