(1)使用布局文件
<1>在Activity的布局文件中:
<1>在Activity的布局文件中:
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.moliying.fragment.MenuFragment"
android:id="@+id/menu_fragment"/>
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:name="com.moliying.fragment.ContentFragment"
android:id="@+id/content_fragment"/>
<2>
创建MenuFragment类,继承Fragment
public class MenuFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onPause() {
super.onPause();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_layout,container,false);
return view;
}
}
//menu_layout.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:id="@+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色"
android:id="@+id/button5" />
</LinearLayout>
创建ContentFragment
public class ContentFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onPause() {
super.onPause();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_layout,container,false);
return view;
}
}
content_layout布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="not data"
android:textSize="50sp"
android:gravity="center"
android:id="@+id/textView_data"
android:layout_gravity="center_horizontal" />
</LinearLayout>
(2)使用代码
<1>在Activity的布局中定义一个布局容器组件,用于添加Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.moliying.fragment.Main2Activity">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/menu_fragment"
android:layout_weight="1"
android:name="com.moliying.fragment.MenuFragment"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/content_frameLayout"
android:layout_weight="3"></FrameLayout>
</LinearLayout>
<2>在Activity中创建Fragment对象,并添加到content_frameLayout容器中:
</pre><pre name="code" class="java"> ContentFragment contentFragment = new ContentFragment();
//添加fragment到指定布局
private void add() {
FragmentManager fm = getFragmentManager();
// android.support.v4.app.FragmentManager f = getSupportFragmentManager();
//开始事务
FragmentTransaction ft = fm.beginTransaction();
//添加(容器布局ID,要添加的Fragment)
ft.add(R.id.content_frameLayout,contentFragment);
//fragmentTransaction.replace(R.id.frame_layout,contentFragment);//添加或替换
// fragmentTransaction.remove(contentFragment);//删除
ft.commit();//提交事务
}