一; 简单Fragment的使用;
效果图;
代码实现:
1), 创建fragment1.xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#00FF00"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #1" android:textSize="25sp" /> </LinearLayout>
2,创建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" android:background="#FF0000" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="This is fragment #2" android:textSize="25sp" /> </LinearLayout>
3,创建main.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" > <fragment android:name="com.example.frament.Frament1" 类名的完整地址 android:id="@+id/frament1" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent" /> <fragment android:name="com.example.frament.Frament2" 类名的完整地址 android:id="@+id/fragment2" android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent" /> </LinearLayout>
4,在com.example.frament包下创建Fragment1
package com.example.frament; 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 Frament1 extends Fragment { @SuppressLint("NewApi") @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.frament1, container,false); } }
5,在com.example.frament包下创建Fragment2
package com.example.frament; 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 Frament2 extends Fragment { @SuppressLint("NewApi") @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.frament2, container,false); } }
6,创建Activity的类MainActivity1(启动类)
public class MainActivity1 extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
分析:
使用fragment来添加碎片需要使用一个类继承Fragment
public class Frament2 extends Fragment { 在ui中绘制,需要重写onCreateView()方法,该方法返回一个View对象,如下所示;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.frament2, container,false); }
使用LayoutInflater 制定布局文件,container参数引用父类 即准备碎片活动 ,savedInstanceState 碎片可以还原到前一次的状态
二;Fragment的动态添加
前五步的代码与上面一样
6,创建Activity的类MainActivity1(启动类)
package com.example.frament; import android.annotation.SuppressLint; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.Display; public class MainActivity1 extends Activity { @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); //向活动添加碎片,根据屏幕的纵向和横向显示 //1,获取碎片管理器 FragmentManager fragment=getFragmentManager(); //2,碎片的显示需要使用FragmentTransaction类操作 FragmentTransaction transacction=fragment.beginTransaction(); //获取屏幕管理器和默认的显示 Display display=getWindowManager().getDefaultDisplay(); //判断横屏 if(display.getWidth()>display.getHeight()){ //获取java类 Frament1 frament1 = new Frament1(); transacction.replace(android.R.id.content, frament1); }else{ Frament2 frament2 = new Frament2(); transacction.replace(android.R.id.content, frament2); } //使用FragmentTransaction必须要commit transacction.commit(); } }
分析:
向活动添加Fragment需要使用FragmentManager类,
FragmentManager fragment=getFragmentManager();
操作fragment需要使用FragmentTransaction进行碎片的添加,删除等操作
FragmentTransaction transacction=fragment.beginTransaction();
获取手机的屏幕
Display display=getWindowManager().getDefaultDisplay();
当前页面替换成 frament1
transacction.replace(android.R.id.content, frament1);
横屏时;
相关推荐
Android Fragment的简单使用(动态添加)
当滑动下方页面时,标签栏上对应标签会显示出与其他标签不一样的效果。下面就以此为例分析顶部标签栏的基本...下方横线部分可以使用TextView实现。在这里我们采用RadioButton来实现标签栏文字部分,关键代码如下所示。
关于动态添加fragment的简单demo
一个接单的示例 Fragment的动态添加
Android Fragment的简单使用(静态添加)
Android Fragment的简单使用,通过动态的方法添加Fragment,通过切换横竖屏显示不同的Fragment
Fragment在xml中,代码添加Fragment,VersionName,Sd卡信息,屏幕宽高
Android Fragment的简单使用,在一个Activity中添加两个Fragment,并让这两个Fragment平分Activity空间,是通过静态的方法添加完成的。
使用ViewPager + Fragment实现滑动切换页面的简单样例
fragment的demo,很简单使用,里面换有tablayout的demo,
你可以在一个单独的activity上把多个fragment组合成为一个多区域的UI,并且可以在多个activity中再使用。你可以认为fragment是activity的一个模块零件,它有自己的生命周期,接收它自己的输入事件,并且可以在...
( 2 ) android.support.v4.app.Fragment:可以兼容到1.6的版本,不能使用<fragment>标签,需要在程序中通过add或者replace的方式添加,同 时还需要 android-support-v4.jar fragment的使用教程请大家参考官网的...
简单的实现了如上要求
随着Android系统的多样化,不仅仅在手机上,在平板、... 我们可以把Fragment看作是Activity的一个界面或者组成部分,而且Fragment具有与Activity很相似的生命周期,我们可以在Activity中动态的添加或者移除某个Fragment。
ViewPager+Fragment_滚动条的实现,很简单,一看就会
简单几行代码,实现Fragment之间切换动画。文章地址: http://blog.csdn.net/abc6368765/article/details/51252481
android.app.Fragment包下的fragment的简单使用,里面有静态添加方法和动态添加方法,适用于各种版本的Android stduio。因为是原生的fragment,所以上手比较容易,适用于刚开始学习Android开发的初学者。大多注释都...
基础的fragment添加,切换fragment,和返回操作
Shatter是一个代替fragment来划分ui模块的库。它主要完成的工作是管理ui区块,并且能和activity保持完全相同的生命周期,没有任何学习成本。 Shatter对于单页面多ui模块的结构有着很好的支持,非常适合用来降低复杂...
简单完成了ViewPager实现轮播图并添加了状态切换的小圆点