步骤
- 新建主layout包含两个RelativeLayout布局
- 新建2个Fragment类分别对应2个layout
- 在主activity中进行动态添加
- 获取FragmentManager对象
- 获取FragmentTransaction对象
- 添加Fragment对象
- 提交事务
运行效果:
主要代码:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > </RelativeLayout> <RelativeLayout android:id="@+id/relativeLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/relativeLayout1" > </RelativeLayout> </RelativeLayout>
activity_fragment1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#FFFF6F" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
activity_fragment2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#ffa6ff" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
MainActivity.java
package com.example.android_fragment2; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1.获取FragmentManager对象 FragmentManager manager = getFragmentManager(); //2.获取FragmentTransaction对象 FragmentTransaction transaction = manager.beginTransaction(); //添加Fragment对象 transaction.add(R.id.relativeLayout1, new Fragment1(), "fragment1"); transaction.add(R.id.relativeLayout2, new Fragment2(), "fragment2"); //4.提交 transaction.commit(); } }
Fragment1.java
package com.example.android_fragment2; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.activity_fragment1, container, false); } }
Fragment2.java
package com.example.android_fragment2; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.activity_fragment2, container, false); } }
赵雅智_fragment动态添加实现,布布扣,bubuko.com
时间: 2024-10-01 03:18:43