本文介绍了如何将活动的主题更改为android中的片段更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用ViewPager在活动中的片段之间切换。每个片段都有不同的背景颜色,所以我设置了不同的主题。我知道我应该在Hi, I am using ViewPager to switch between fragments in an activity. Each fragment has its different background color, so I set different Themes. I know i'm supposed to set an activity's Theme beforesetContentView(R.layout.activity_walk_through);之前设置一个活动的主题。但它有点棘手。当活动的当前项目设置为片段时,如何为活动设置不同的主题。这是我的代码 我尝试过: 活动的活动. But it's kinda tricky. How do I set different themes for the activity when activity's current item is set to a fragment. Here's my codeWhat I have tried:Activity's activityViewPagerAdapter viewPagerAdapter;ViewPager mViewPager;static final String KEY_IS_FIRST_TIME = "com.geeks14.sureremit.first_time";static final String KEY = "com.com.geeks14.sureremit";@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_walk_through); viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); viewPagerAdapter.addFragment(new WalkThroughOneFragment(), "Walkthrough One"); viewPagerAdapter.addFragment(new WalkThroughTwo(), "Walkthrough Two"); viewPagerAdapter.addFragment(new WalkThroughThreeFragment(), "Walkthrough Three"); mViewPager = (ViewPager)findViewById(R.id.pager); mViewPager.setAdapter(viewPagerAdapter); setTitle("");}public void setCurrentItem (int item, boolean smoothScroll) { mViewPager.setCurrentItem(item, smoothScroll);}class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> fragmentList = new ArrayList<>(); private final List<String> fragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager){ super(manager); } @Override public Fragment getItem(int position){ return fragmentList.get(position); } @Override public int getCount(){ return fragmentList.size(); } public void addFragment(Fragment fragment, String title){ fragmentList.add(fragment); fragmentTitleList.add(title); } @Override public CharSequence getPageTitle(int position){ return fragmentTitleList.get(position); }} 这是我片段之一的活动Here's one of my fragment's activitypublic static WalkThroughOneFragment newInstance(String param1, String param2) { WalkThroughOneFragment fragment = new WalkThroughOneFragment(); Bundle args = new Bundle(); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);//i set activity theme here but it did nothing } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.fragment_walk_through_one, container, false); nextButton = (FontButton)v.findViewById(R.id.FragmentOneNextButton); nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ((WalkThroughActivity)getActivity()).setCurrentItem(1, true);//fragment is set here } }); return v; }推荐答案好的,经过大量试用,我终于找到了这个这里 我所要做的就是隐藏状态栏,这样,每个片段都会占据全屏。我通过将下面的代码放在活动中隐藏状态栏我在我的片段之前在setContentView()之前Ok, after lots of trial, i finally found this hereAll i had to do was hide the status bar, that way, each fragments take up fullscreen. I hid the status bar by placing the code below in the activity i had my fragments in before setContentView()View decorView = getWindow().getDecorView(); // Hide the status bar. int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); 这篇关于如何将活动的主题更改为android中的片段更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 20:08