我在ActionBar
和自定义Activity
中使用ActionBar
,并为每个Fragment
使用单独的布局文件。我在Fragment
FrameLayout
个MainActionBarActivity.java
个
public class MainActionBarActivity extends ActionBarActivity implements OnHeadlineSelectedListener,UserToProfileFragmentInterface {
// code here...
}
ProfileFragment.java
同样,其中一个片段是public class ProfileFragment extends Fragment {
Context context;
public final String TAG = "ProfileFragment";
FrameLayout fragmentContainer;
private int mCurrentlyShowingFragment;
public final String SAVED_STATE_KEY = ProfileFragment.class.getSimpleName();
public static ProfileFragment newInstance(Fragment.SavedState savedState) {
ProfileFragment frag = new ProfileFragment();
frag.setInitialSavedState(savedState);
return frag;
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.temp_profile, container, false);
context = getActivity();
fragmentContainer = (FrameLayout)view.findViewById(R.id.flayout);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState == null) {
getChildFragmentManager().beginTransaction().add(R.id.flayout,ProfileFragmentDetails.newInstance()).addToBackStack("").commit();
// setRetainInstance(true);
mCurrentlyShowingFragment = 0;
} else {
mCurrentlyShowingFragment = savedInstanceState.getInt("currently_showing_fragment");
}
}
Fragment
这只是替换新的ProfileFragmentDetails
- Fragment
。
现在正在ActionBar
我尝试使用自定义onResume()
来编写代码@Override
public void onResume(){
Log.d(TAG, "onResume;");
super.onResume();
Log.d(TAG, "mSingleTon.profileUpdated:" + mSingleTon.profileUpdated);
if(getActivity().getSupportFragmentManager().getBackStackEntryCount()>0){
ActionBar actionBar = ((MainActionBarActivity) getActivity()).getSupportActionBar();
((MainActionBarActivity) getActivity()).invalidateOptionsMenu();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
View mCustomView = LayoutInflater.from(context).inflate(R.layout.actionbar_profile_view, null);
actionBar.setCustomView(R.layout.actionbar_profile_view);
actionBar.setDisplayShowCustomEnabled(true);
Toolbar parent =(Toolbar) mCustomView.getParent();//first get parent toolbar of current action bar
if(parent != null)
{
parent.setContentInsetsAbsolute(0,0);// set padding programmatically to 0dp
}
View v1 = actionBar.getCustomView();
imageViewBroadcastBack = (ImageView)v1.findViewById(R.id.imageViewBroadcastBack);
imageViewProfileSetting = (ImageView)v1.findViewById(R.id.imageViewProfileSetting);
textViewScreenTitle = (TextView)v1.findViewById(R.id.textViewScreenTitle);
textViewScreenTitle.setTypeface(Typeface.createFromAsset(context.getAssets(), "HelveticaBold.ttf"));
textViewScreenTitle.setText("Profile");
}
else
{
//getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
//userProfileResponse = null;
/*
// Clear all back stack.
int backStackCount = getActivity().getSupportFragmentManager().getBackStackEntryCount();
for (int i = 0; i < backStackCount; i++) {
// Get the back stack fragment id.
int backStackId = getActivity().getSupportFragmentManager().getBackStackEntryAt(i).getId();
getActivity().getSupportFragmentManager().popBackStack(backStackId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
// end of for
*/
this.uid = mSingleTon.userResponse.getUser().getUid();
ActionBar actionBar = ((MainActionBarActivity) getActivity()).getSupportActionBar();
((MainActionBarActivity) getActivity()).invalidateOptionsMenu();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
View mCustomView = LayoutInflater.from(context).inflate(R.layout.actionbar_profile_view, null);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(R.layout.actionbar_profile_view);
actionBar.setDisplayShowCustomEnabled(true);
Toolbar parent = (Toolbar) mCustomView.getParent();
if(parent != null)
{
parent.setContentInsetsAbsolute(0, 0);
}
//actionBar.setCustomView(R.layout.actionbar_profile_view);
View v1 = actionBar.getCustomView();
imageViewBroadcastBack = (ImageView)v1.findViewById(R.id.imageViewBroadcastBack);
textViewScreenTitle = (TextView)v1.findViewById(R.id.textViewScreenTitle);
textViewScreenTitle.setTypeface(Typeface.createFromAsset(context.getAssets(), "HelveticaBold.ttf"));
imageViewProfileSetting = (ImageView)v1.findViewById(R.id.imageViewProfileSetting);
}
:
null
如果我进入parent.setContentInsetsAbsolute
条件检查,它会在if
这一行给我fillparent
,然后它不会使应用程序崩溃,但它不会将宽度设置为{{1} }。
当我尝试从一个片段移动到另一个片段时,再次通过指示parent.setContentInsetsAbsolute
在同一行Null Pointer Exception
上应用崩溃。
答案 0 :(得分:0)
尝试使用
actionBar.setCustomView(mCustomView);
而不是
actionBar.setCustomView(R.layout.actionbar_profile_view);
提供布局的ID,android会为您提供一个新的视图,该视图与您充气并分配给mCustomView
的视图不同