使用fragment兼容低版本的写法
[1]定义fragment继承V4包中的Fragment
[2]定义的activity要继承v4包中的FragmentActivity
[3]通过这个方法getSupportFragmentManager 获取Fragment的管理者
一、项目目录结构
二、activity_main.xml代码
<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.zgs.FragmentLowVersion.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
三、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="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是竖屏的内容"
android:textColor="#ff0000"
android:textSize="20sp" />
</LinearLayout>
四、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="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是横屏的内容" />
</LinearLayout>
五、MainActivity.java代码
package com.zgs.FragmentLowVersion;
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.WindowManager;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// [1]获取手机的宽和高 windommanager
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
// [2]判断横竖屏
// [3.1]如果使用v4包中的fragment 获取fragment的管理者 是通过getsupportFragmentManager();
FragmentManager supportFragmentManager = getSupportFragmentManager();
// [3.2]开启一个事务
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
if (height > width) {
// 说明是竖屏 androind 代表系统定义好的 android.R.id.content理解成是当前手机的窗体
transaction.replace(android.R.id.content, new Fragment1());
} else {
// 横屏
transaction.replace(android.R.id.content, new Fragment2());
}
//[4]一定要记得 提交commit
transaction.commit();
}
}
六、Fragment1.java代码
package com.zgs.FragmentLowVersion;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//定义一个Fragment
public class Fragment1 extends Fragment {
//当用户第一次画ui的时候调用 要显示Fragment自己的内容 setContentView(R.layout.activity_main);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//[1]通过打气筒把一个布局转换成view对象
View view = inflater.inflate(R.layout.fragment1, null);
return view;
}
}
七、Fragment2.java代码
package com.zgs.FragmentLowVersion;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//定义一个Fragment
public class Fragment2 extends Fragment {
//当用户第一次画ui的时候调用 要显示Fragment自己的内容 setContentView(R.layout.activity_main);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//[1]通过打气筒把一个布局转换成view对象
View view = inflater.inflate(R.layout.fragment2, null);
return view;
}
}