Fragment加入到Activity的两种方式

(1)使用布局文件

<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();//提交事务
    }




  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值