Fragment在xml中使用

Demo下载地址:http://download.csdn.net/detail/baopengjian/9336883

Fragment在xml中使用,Fragment之间通信


(1)FirstMethodActivity

publicclassFirstMethodActivityextendsFragmentActivity{


@Override
protectedvoidonCreate(Bundlearg0){
super.onCreate(arg0);
setContentView(R.layout.activity_first);
}
}

activity_first.xml:
<RelativeLayoutxmlns:android=" http://schemas.android.com/apk/res/android "
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bpj.fragmentdemo.MainActivity">

<fragment
android:id="@+id/fg1"
android:name="com.bpj.fragmentdemo.fragment.FirstMethodFragment1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/first_method"/>

<fragment
android:layout_below="@id/fg1"
android:id="@+id/fg2"
android:name="com.bpj.fragmentdemo.fragment.FirstMethodFragment2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/first_method"/>

</RelativeLayout>
(2)Fragment2:
publicclassFirstMethodFragment2extendsFragment{

privateContextcontext;
privateTextViewtv_fragment1,tv_fragment2,tv_fragment3;
privateStringversionName,VersionCode;
privateintscreenWidth;
privateintscreenHeight;
privateFilesdCardDir;
privatelongtotalSpace;
privateStringtotalSize,usableSize;

@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
Viewview=inflater.inflate(R.layout.fragment_first2,null);


tv_fragment1=(TextView)view.findViewById(R.id.tv_fragment1);
tv_fragment2=(TextView)view.findViewById(R.id.tv_fragment2);
tv_fragment3=(TextView)view.findViewById(R.id.tv_fragment3);
returnview;
}

@Override
publicvoidonActivityCreated(BundlesavedInstanceState){
super.onActivityCreated(savedInstanceState);
context=getActivity();
try{
PackageInfoinfo=context.getPackageManager().getPackageInfo(context.getPackageName(),0);
versionName=info.versionName;
VersionCode=info.versionCode+"";
}catch(NameNotFoundExceptione){
}

displayMetricsmetrics=newdisplayMetrics();
getActivity().getwindowManager().getDefaultdisplay().getMetrics(metrics);
screenWidth=metrics.widthPixels;
screenHeight=metrics.heightPixels;

if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
sdCardDir=Environment.getExternalStorageDirectory();
totalSpace=sdCardDir.getTotalSpace();
totalSize=Formatter.formatFileSize(context,totalSpace);
usableSize=Formatter.formatFileSize(context,sdCardDir.getUsableSpace());
}

}

publicvoidsetText(){
tv_fragment1.setText("versionName:"+versionName+";versionCode:"+VersionCode);
tv_fragment2.setText("screenWidth:"+screenWidth+";screenHeight:"+screenHeight);
tv_fragment3.setText("sdDir:"+sdCardDir.getPath()+"\n"+"totalSize="+totalSize+";usableSize="+usableSize);
}

publicvoidclear(){
tv_fragment1.setText(null);
tv_fragment2.setText(null);
tv_fragment3.setText(null);
}
}

fragment_first2.xml:
<RelativeLayoutxmlns:android=" http://schemas.android.com/apk/res/android "
android:layout_width="match_parent"
android:layout_height="600dp"
android:background="@android:color/holo_blue_light"
tools:context="com.bpj.fragmentdemo.MainActivity">

<TextView
android:id="@+id/tv_fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="14dp"
android:text="Fragment2_1"/>

<TextView
android:id="@+id/tv_fragment3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_fragment2"
android:layout_below="@+id/tv_fragment2"
android:layout_marginTop="105dp"
android:text="Fragment2_3"/>

<TextView
android:id="@+id/tv_fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tv_fragment1"
android:layout_below="@+id/tv_fragment1"
android:layout_marginTop="100dp"
android:text="Fragment2_2"/>

</RelativeLayout>

(3)Fragment1:
publicclassFirstMethodFragment1extendsFragmentimplementsOnClickListener{

privateTextViewtv1,tv2;
privateFirstMethodFragment2mFragment2;

@Override
publicViewonCreateView(LayoutInflaterinflater,
BundlesavedInstanceState){
Viewview=inflater.inflate(R.layout.fragment_first1,null);

tv1=(TextView)view.findViewById(R.id.tv1);
tv1.setonClickListener(this);
tv2=(TextView)view.findViewById(R.id.tv2);
tv2.setonClickListener(this);
returnview;
}

@Override
publicvoidonActivityCreated(BundlesavedInstanceState){
super.onActivityCreated(savedInstanceState);
mFragment2=(FirstMethodFragment2)getActivity().getSupportFragmentManager().findFragmentById(R.id.fg2);
}

@Override
publicvoidonClick(Viewview){
switch(view.getId()){
caseR.id.tv1:
mFragment2.setText();
break;
caseR.id.tv2:
mFragment2.clear();
break;
}
}
}
<RelativeLayoutxmlns:android=" http://schemas.android.com/apk/res/android "
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bpj.fragmentdemo.MainActivity">

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="42dp"
android:text="Fragment1"/>

<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv1"
android:layout_marginLeft="23dp"
android:layout_toRightOf="@+id/tv1"
android:text="Fragment2"/>

</RelativeLayout>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇