首页 > 移动开发 > Fragment 横竖屏切换时加载不同的布局

Fragment 横竖屏切换时加载不同的布局

1、创建两个FragmentActivity 让它继承 Fragment ,这里最低版本为11

 

[java][/java] view plaincopyprint?

  1. package com.example.fragment;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class Fragment1 extends Fragment {
  8.     @Override
  9.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10.             Bundle savedInstanceState) {
  11.         // TODO Auto-generated method stub
  12.         return inflater.inflate(R.layout.activity_fragment1, null);
  13.     }
  14. }

 

 

[java][/java] view plaincopyprint?

  1. package com.example.fragment;
  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class Fragment2 extends Fragment {
  8.     @Override
  9.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10.             Bundle savedInstanceState) {
  11.         // TODO Auto-generated method stub
  12.         return inflater.inflate(R.layout.activity_fragment2, null);
  13.     }
  14. }

2、创建两个.xml 文件,用来显示屏幕切换时所用到的布局

 

 

 

[html][/html] view plaincopyprint?

  1. activity_fragment1.xml
[html][/html] view plaincopyprint?

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="@drawable/ic_launcher"
  6.     tools:context=".MainActivity" >
  7. </RelativeLayout>
[html][/html] view plaincopyprint?

  1. activity_fragment2.xml
[html][/html] view plaincopyprint?

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#0000ff"
  6.     tools:context=".MainActivity" >
  7. </RelativeLayout>

 

 

3、在程序中调用

 

[java][/java] view plaincopyprint?

  1. package com.example.fragment;
  2. import android.app.Activity;
  3. import android.app.FragmentManager;
  4. import android.app.FragmentTransaction;
  5. import android.os.Bundle;
  6. public class MainActivity extends Activity {
  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.         int windowHeight = this.getResources().getDisplayMetrics().heightPixels; //获取当前屏幕的高
  12.         int windowWidth = this.getResources().getDisplayMetrics().widthPixels; //获取当前屏幕的宽
  13.         Fragment1 f1 = new Fragment1();
  14.         Fragment2 f2 = new Fragment2();
  15.         FragmentManager fm = getFragmentManager();
  16.         FragmentTransaction ft = fm.beginTransaction();
  17.         if(windowWidth > windowHeight){          //横屏
  18.             ft.replace(android.R.id.content, f1); //是横屏的时候显示f1的布局
  19.         }else {
  20.             ft.replace(android.R.id.content, f2); //显示f2 中的布局
  21.         }
  22.         ft.commit();
  23.     }
  24. }

本文固定链接: http://www.devba.com/index.php/archives/3313.html | 开发吧

报歉!评论已关闭.