接下来我们使用动态的方式调用fragment,在这里我们使用了WindowManager这个类,我们可以通过这个类获取到当前设备的宽和高,然后对比宽和高来判断是横屏还是竖屏,然后再确定是调用那个fragment,在这里我们也使用到了事物(FragmentTransaction),事物就是要么同时成功要么同时失败,然后使用FragmentTransaction的replace方法进行替换。接下来我们用代码进行演示。
1 创建一个android工程,我这里的工程如下
2 创建两个类继承Fragment这两个类的代码如下
VerticalScreenFragment代码如下
package cn.wxz.fragment;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class VerticalScreenFragment extends Fragment {
public VerticalScreenFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.verticalscreen,null);
return view;
}
}这个类对应的布局文件verticalscreen.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="match_parent"
android:text="我是竖屏------------------"
/>
</LinearLayout>
CrossScreenFragment类代码如下
package cn.wxz.fragment;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class CrossScreenFragment extends Fragment {
public CrossScreenFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.crossscreen,null);
return view;
}
}对应的布局文件crossscren.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_height="match_parent"
android:layout_width="match_parent"
android:text="我是横屏"
android:textSize="30dp"
/>
</LinearLayout>MainActivity类的代码如下:
package cn.wxz.fragment;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.WindowManager;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
@SuppressWarnings("deprecation")
@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]获取fragment的管理者
FragmentManager manager = getFragmentManager();
// [3.2]开启一个事务
FragmentTransaction transaction = manager.beginTransaction();
if (height > width) {
// 说明是竖屏 androind 代表系统定义好的 android.R.id.content理解成是当前手机的窗体
transaction.replace(android.R.id.content, new VerticalScreenFragment());
} else {
// 横屏
transaction.replace(android.R.id.content, new CrossScreenFragment());
}
//[4]一定要记得 提交commit
transaction.commit();
}
}
运行模拟器结果如下
当我按下ctrl+F11进行横竖屏切换后,如下
大家一定要掌握这种方式调用fragment这种方式在开发中很常用。
988

被折叠的 条评论
为什么被折叠?



