Add Fragment to Activity:

There are two ways to add Fragment to Activity:

  1. Add directly to the layout file and use the Fragment as part of the entire layout of the Activity.Add Fragment
    directly to the layout file. You can use the fragment /fragment tag to achieve.
    In the fragment /fragment tag, the android:name attribute is used to specify the Fragment to be added
  2. When the Activity is running, put the Fragment into the Activity layout.
    When the Activity is running, you can also add the Fragment to the Activity's layout. The implementation method is: Get an instance of FragmentTransaction, and then use the add() method to add a Fragment, add( ) The first parameter of the method is the ViewGroup (specified by the Resource ID) to be placed in the Fragment, and the second parameter is the Fragment that needs to be added. Finally, in order for the change to take effect, the commit() method must be called to submit the transaction.

Example: To add a Fragment named DetailFragment when the Activity is running:

//实例化DetailFragment的对象
DetailFragment details = new DetailFragment();
//获得一个FragmentTransaction的实例
	FragmentTransaction ft = getFragmentManager()
						.beginTransaction();
	//添加一个显示详细内容的Fragment
	ft.add(android.R.id.content,details);
	//提交事务
	ft.commit();

One of the more powerful features of Fragment is that it can merge two activities so that these two activities can be displayed on one screen.

定义View.OnClickListener对象并重写onClick()方法:
	View.OnClickListener l = new View.OnClickListener() {
		@Override
		public void onClick(View v)
		{
			//代码
			}
			}

Guess you like

Origin blog.csdn.net/qq_42823109/article/details/94361800