android兼容低版本Fragment
来源:互联网 发布:can总线控制伺服编程 编辑:程序博客网 时间:2024/05/06 04:21
使用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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// [1]获取手机的宽和高 windommanagerWindowManager 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);@Overridepublic 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);@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//[1]通过打气筒把一个布局转换成view对象 View view = inflater.inflate(R.layout.fragment2, null);return view;}}
0 0
- android兼容低版本Fragment
- Android Fragment兼容低版本
- 低版本兼容使用Fragment
- 使用fragment兼容低版本的写法
- android studio更改兼容android低版本
- Android开发兼容低版本sdk问题
- android版本兼容,高版本低版本兼容性
- android 兼容包(可兼容低版本系统)
- android 兼容包(可兼容低版本系统)
- Fragment的使用详解 低版本兼容 回退栈 相互通信 Fragment+RadioButton
- Android 应用程序向低版本兼容的问题
- android仿holo主题兼容低版本时间控件
- Android好奇宝宝_07_ViewPager切换动画(兼容低版本)
- android api实现高斯模糊,且兼容低版本
- android ActionBar 兼容低版本之搜索功能
- android状态栏一体化、沉浸式状态栏(兼容低版本)
- android状态栏一体化、沉浸式状态栏(兼容低版本)
- android sdk 兼容低版本的处理方法
- RecyclerView再学习
- 永不止步
- 再见,2016(本兮)
- Mybatis续、Mapper映射器接口规则
- JavaScript:直接写入 HTML 输出流
- android兼容低版本Fragment
- 【求助】android 用布局界面 button布局 按下响应系统按键事件
- Android Studio的一个问题
- 类型 FragmentTransaction 中的方法 replace(int, Fragment)对于参数(int, Fragment1)不适用
- svn使用
- hdu 3350 #define is unsafe
- 第一篇博客--总结2016--
- (11)图案填充
- Mac OS X 中一些常用的命令行技巧