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 方法。这里是新代码。有什么想法吗?还要注意我已将我的 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;
    }
}

}

4

1 回答 1

0

如果我做出不正确的假设,请纠正我,因为我不能 100% 确定您要达到的目标。

我建议在 onActivityCreated() 方法中设置 TextView,因为您无法访问片段的 onCreate() 中的根视图。修改 WelcomePage 片段,如下所示:

更新了将应用程序上下文的引用传递给 WelcomePage 的更改。

public class MainActivity extends FragmentActivity {
    Context appContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        appContext = getApplicationContext();
    }

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(appContext);  
        case 1:  
            return fragment = new Facebook();   
        default:  
            break;  
        }  
        return fragment;
    }
}


public static class WelcomePage extends Fragment {

    private View rootView;
    private Context context;

    public WelcomePage(Context c) {
        context = c;
    }

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_main_two, container, false);
        return rootView;
    }

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Typeface ralewayFont = Typeface.createFromAsset(context.getAssets(), "fonts/raleway.ttf");
        Typeface abeatFont = Typeface.createFromAsset(context.getAssets(), "fonts/abeat.otf");

        TextView hiText = (TextView) rootView.findViewById(R.id.hi);
        TextView welcomeText = (TextView) rootView.findViewById(R.id.welcome);
        TextView chancechatText = (TextView) rootView.findViewById(R.id.chancechat);

        hiText.setTypeface(ralewayFont);
        welcomeText.setTypeface(ralewayFont);
        chancechatText.setTypeface(abeatFont);
    }
}
于 2013-06-30T21:21:11.137 回答