gpt4 book ai didi

java - 标题不显示在 SlidingTabLayout 中

转载 作者:行者123 更新时间:2023-12-02 05:12:40 26 4
gpt4 key购买 nike

我尝试在我的应用程序中实现 SlidingTabLayout。遗憾的是它没有在滑动选项卡中显示标题。我需要在选项卡中显示我的 Activity 的标题(稍后在图标中)。我可以知道该怎么做吗?我对 Android 编程很陌生。这是我的代码:

MainMenuActivity.java

import com.example.android.common.view.SlidingTabLayout;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainMenuActivity extends ActionBarActivity {

private PagerAdapter mPagerAdapter;
private SlidingTabLayout mSlidingTabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_layout);

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}

List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this,EducationActivity.class.getName()));
fragments.add(Fragment.instantiate(this,EducationActivity.class.getName()));

mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setViewPager(pager);
}

@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, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
else if (id == R.id.action_disclaimer) {
Intent intent = new Intent(this, DisclaimerActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}

PagerAdapter.java

import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class PagerAdapter extends FragmentPagerAdapter {

private List<Fragment> fragments;

public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
// TODO Auto-generated constructor stub
this.fragments = fragments;
}

@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub

return this.fragments.get(arg0);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return this.fragments.size();
}

}

教育 Activity .java

public class EducationActivity extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub

View v = inflater.inflate(R.layout.activity_education,
container,false);

Button btn_warfarin = (Button)v.findViewById(R.id.btn_warfarin);
btn_warfarin.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getActivity(), WarfarinInfoActivity.class);
startActivity(intent);

}

});
}

我已经阅读了 http://developer.android.com/ 中的 SlidingTabsBasicFragment.java但我不太明白。希望有人能指导我。谢谢。

最佳答案

你错过了

@Override
public CharSequence getPageTitle(int position) {

return myStringArrayOfTabs[position];
}

从您的适配器中,根据需要填写标题。

关于java - 标题不显示在 SlidingTabLayout 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27209208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com