Fragment使用V4包的注意事项

1.使用V4包下的Fragment, FragmentManager也需要从v4中获取
2.当前Activity要使用FragmentActivity
3.通过getSupportFragmentManager()获取FragmentManager对象

注意:

  1. Android Support v4: 1.这个包是为了照顾1.6及更高版本而设计的 这个包是使用最广泛的 eclipse新建工程时 默认带有
  2. Android Support v7: 这个包是为了考虑照顾2.1及以上版本而设计的 v7是依赖于v4包的
  3. Android Support v13:这个包是为了考虑照顾3.2及以上版本而设计的 我们平常手机开发不常用,平板中能用到

MainActivity

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
//使用V4包时继承的是FragmentActivity
public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //使用V4包,用getSupportFragmentManager()得到FragmentManager的对象
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.layout_container_fragment, new MyFragment());
        transaction.commit();       
    }   
}

自定义MyFragment继承Fragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_fragmnet, container, false);
    }
}

MyFragment布局

<?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="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
  <TextView 
      android:id="@+id/fragment_v4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="我是V4包Fragment"
      android:background="#50ff"
      />  
</LinearLayout>

activity_main布局

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ljavadroid.day16_fragmentv4.MainActivity" 
    >

    <LinearLayout
        android:id="@+id/layout_container_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#50ff"
        />
</RelativeLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容