加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Fragment在xml中使用

发布时间:2020-12-16 08:48:40 所属栏目:百科 来源:网络整理
导读:Demo下载地址:http://download.csdn.net/detail/baopengjian/9336883 Fragment在xml中使用,Fragment之间通信 (1) FirstMethodActivity publicclassFirstMethodActivityextendsFragmentActivity{ @Override protectedvoidonCreate(Bundlearg0){ super.onC

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 "
xmlns:tools=" http://schemas.android.com/tools"
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){
e.printStackTrace();
}

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 "
xmlns:tools=" http://schemas.android.com/tools"
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 "
xmlns:tools=" http://schemas.android.com/tools"
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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读