What is the difference between Acitvity_Main.xml and Fragment_main.xml files in Android Studio

后端 未结 3 1393
醉梦人生
醉梦人生 2020-12-14 16:20

When ever i create a new project, the Fragment_main.xml file is added to my Layout folder and unlike in Eclipse it is this file that contains what is normally in the Activit

相关标签:
3条回答
  • 2020-12-14 16:55

    The Activity_main.xml contains the Layout for the FragmentActivity and the fragment_main.xml is the Layout for the fragment.

    For more information to fragments and how you can use it.
    Visit: http://developer.android.com/training/basics/fragments/index.html

    0 讨论(0)
  • 2020-12-14 16:58

    If you are creating a new Project and it adds fragment_main.xml by default you must be selecting a layout by default. Maybe a pager/spinner layout?

    Fragment_main is the same as activity_main. The names are just string labels and mean nothing in and of itself and are just changed for clarity by the IDE.

    Have a read of this.

    http://developer.android.com/guide/topics/ui/declaring-layout.html

    0 讨论(0)
  • 2020-12-14 17:12

    just as Bytehawks said above.

    activity_main.xml describes Layout for the FragmentActivity and the fragment_main.xml is the Layout for the fragment.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //get the activity_main.xml for layout
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    
        //code for describing layout more details, get fragment_main.xml
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }
     /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
    
        public PlaceholderFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
    
    0 讨论(0)
提交回复
热议问题