Android 实现片段后无法以编程方式编辑文本
这是背景资料 使用此代码编辑具有某些自定义属性(自定义字体、图像等)的文本和图像 这是一个小摘录。它被置于主要活动的onCreate方法下 现在我已经实现了这样的片段。我在哪里实现代码。(我尝试了旧代码和WelcomePage方法。这里是新代码。有什么想法吗?还要注意的是,我已经将EditText从activity_main.xml移动到fragment_main_dummy.xmlAndroid 实现片段后无法以编程方式编辑文本,android,user-interface,fragment,Android,User Interface,Fragment,这是背景资料 使用此代码编辑具有某些自定义属性(自定义字体、图像等)的文本和图像 这是一个小摘录。它被置于主要活动的onCreate方法下 现在我已经实现了这样的片段。我在哪里实现代码。(我尝试了旧代码和WelcomePage方法。这里是新代码。有什么想法吗?还要注意的是,我已经将EditText从activity_main.xml移动到fragment_main_dummy.xml public class MainActivity extends FragmentActivity { /*
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;
}
}
}如果我做出了错误的假设,请纠正我,因为我不能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);
}
}
无法获取资产,因为WelcomePage是一个静态类。有什么想法吗?我相信您的假设是正确的。我更新了我的答案,做了一些更改。我不确定这是否是最好的解决方案,但它应该可以工作。基本上,因为WelcomePage类不扩展活动,并且是静态的,所以您无法直接访问a应用程序上下文。因此,我们需要在主活动中保存对它的引用,然后在WelcomePage片段实例化时传递它。查看onActivityCreated(Bundle savedInstanceState){因此此行在Eclipse中引发错误。它表示“返回类型与fragment.onActivityCreated(Bundle)不兼容”有什么想法吗?感谢到目前为止的帮助,我很感激。是的,简单的错误。将其更改为public void on activitycreated(Bundle savedInstanceState)。
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);
}
}