看看這個例子: https://stackoverflow.com/a/24042109/2413303
基本上,指定碎片構造函數中的資源ID:
public MyFragment()
{
super();
titleId = R.string.drawer_myfragment_title;
}
添加如下界面(在編輯加):
public interface GetActionBarTitle
{
int getActionBarTitleId();
}
返回這與接口:
public class MyFragment extends Fragment implements GetActionBarTitle
{
private int titleId;
@Override
public int getActionBarTitleId()
{
return titleId;
}
}
而在活動中,添加以下到onCreate()
方法:
getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener()
{
public void onBackStackChanged()
{
int backCount = getSupportFragmentManager().getBackStackEntryCount();
if (backCount == 0)
{
finish();
}
else
{
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
mTitle = getString(((GetActionBarTitle) fragment).getActionBarTitleId());
getSupportActionBar().setTitle(mTitle);
}
}
});
implements GetActionBarTitle給我錯誤,我不能使用它。 – 2014-09-08 10:24:10
添加了接口代碼 – EpicPandaForce 2014-09-08 10:47:17