About activity_main.xml and fragment_main.xml, activity_main.xml

Source: Internet
Author: User

About activity_main.xml and fragment_main.xml, activity_main.xml

Solution 1

The new version of the SDK has two XML files, activity_main.xml and fragment_main.xml,
You can do this if you are not used to it:
1. Delete the entire fragment_main.xml File
2. Delete the content in activity_main.xml. Switch to Graphy Layout and put a LinearLayout.
3. For MainActivity. java, you can delete some content and change MainActivity extends ActionBarActivity to MainActivity extends Activity:
The change is as follows:
Package com. zmcorp. weightcalculator;

Import android. OS. Bundle;
Import android. view. LayoutInflater;
Import android. view. Menu;
Import android. view. MenuItem;
Import android. view. View;
Import android. view. ViewGroup;
Import android. OS. Build;

MainActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );


}

@ 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 );
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;
}
Return super. onOptionsItemSelected (item );
}


}

}

Method 2 ::

MyTextview and myButton are in fragment_main.xml. Before fragment_main.xml is introduced, you cannot find them through findViewById ..

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 );
TextView textview = (TextView) rootView. findViewById (R. id. view );
System. out. println (textview );
Testview. setText ("Hello Android! ");

Return rootView;
}
}


For new Android users, fragment_mainxml and Activity_mainxml

SetContentView (R. layout. fragment_main)

In the latest android version, how does one operate components in fragment_mainxml in MainActivityjava?

You can write this in the onStart () method.
Fragment is the fragment object added to your activity. button = (Button) fragment. getView (). findViewById (R. id. button1 );
Button. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){

}
});

You will not report an error.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.