Home > Mobile >  Of fragments don't show the problem
Of fragments don't show the problem

Time:11-25

ViewActivity. Java:
 
Public class ViewActivity extends AppCompatActivity {


Private MainFragment MainFragment;

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
The setContentView (R.l ayout. Activity_view);

//instantiate MainFragment
MainFragment=new mainFragment ();
//add fragments to the container
GetSupportFragmentManager (). The beginTransaction (). The add (R.i d.f l_container, mainFragment). CommitAllowingStateLoss ();
}
}



XML files associated with it, activity_view. XML:
 
<? The XML version="1.0" encoding="utf-8"?>
XMLNS: app="http://schemas.android.com/apk/res-auto"
XMLNS: tools="http://schemas.android.com/tools"
Android: layout_width="match_parent"
Android: layout_height="match_parent"
Tools: context="fragments. ViewActivity" & gt;

Android: layout_width="match_parent"
Android: layout_height="wrap_content"
Android: text="activity_view"
Android: textSize="20 dp"
The android: id="@ + id/tv_4"
Android: textColor="# 000"/& gt;

<frameLayout
Android: layout_width="match_parent"
Android: layout_height="match_parent"
Android: layout_below="@ id/tv_4"
The android: id="@ + id/fl_container"/& gt;




Create MainFragment class
 
Public class MainFragment extends fragments {
Private TextView tv_3;
Private View View;

@ Nullable
@ Override
Public View onCreateView (@ NonNull LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {

View=inflater. Inflate (R.l ayout. Main_fragment, container, false);
Return the view;
}

@ Override
Public void onViewCreated (@ NonNull View View, @ Nullable Bundle savedInstanceState) {
Super. OnViewCreated (view, savedInstanceState);
Tv_3=(TextView) view. The findViewById (R.i which v_3);
}
}


XML files associated with it, XML main_fragment.
 
<? The XML version="1.0" encoding="utf-8"?>
XMLNS: android="http://schemas.android.com/apk/res/android"
Android: layout_width="match_parent"
Android: layout_height="match_parent"
Android: gravity="center" & gt;

Android: layout_width="match_parent"
Android: layout_height="wrap_content"
Android: text="home page"
Android: gravity="center"
Android: textSize="50 dp"
Android: textColor="# 000"
The android: id="@ + id/tv_3"/& gt;




The part of code is like this, but the FrameLayout shows don't come out


Have who can help me see how to solve this problem agghh

CodePudding user response:

No problem, your overall logic beginTransaction (). The add (), has been added to the transaction, but you just add, no call show () method, instead of beginTransaction (). The add (XXX). The show (XXX)
  • Related