Fragment在xml中使用
来源:互联网 发布:大数据时代 企业 编辑:程序博客网 时间:2024/06/06 05:52
Demo下载地址:http://download.csdn.net/detail/baopengjian/9336883
Fragment在xml中使用,Fragment之间通信
(1)FirstMethodActivity
public class FirstMethodActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.activity_first);
}
}
activity_first.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"
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:
public class FirstMethodFragment2 extends Fragment{
private Context context;
private TextView tv_fragment1,tv_fragment2,tv_fragment3;
private String versionName,VersionCode;
private int screenWidth;
private int screenHeight;
private File sdCardDir;
private long totalSpace;
private String totalSize,usableSize;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = 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);
return view;
}
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity();
try {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
versionName = info.versionName;
VersionCode = info.versionCode+"";
} catch (NameNotFoundException e) {
e.printStackTrace();
}
DisplayMetrics metrics = new DisplayMetrics();
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());
}
}
public void setText(){
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);
}
public void clear(){
tv_fragment1.setText(null);
tv_fragment2.setText(null);
tv_fragment3.setText(null);
}
}
fragment_first2.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="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:
public class FirstMethodFragment1 extends Fragment implements OnClickListener {
private TextView tv1, tv2;
private FirstMethodFragment2 mFragment2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = 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);
return view;
}
@Override
public void onActivityCreated( Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mFragment2 = (FirstMethodFragment2) getActivity().getSupportFragmentManager().findFragmentById(R.id.fg2);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv1:
mFragment2.setText();
break;
case R.id.tv2:
mFragment2.clear();
break;
}
}
}
<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"
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>
0 0
- Fragment在xml中使用
- 在Fragment中使用GestureDetector
- 在Fragment中使用Menu
- 在fragment中使用onActivityResult
- 在Fragment中使用Listview
- swipRefresh在fragment中使用
- Android 中使用 XML 中的 fragment 心得
- 在Fragment嵌套Fragment使用中,出现第二次使用空白
- Android中在xml中静态添加Fragment
- 在Activity,Fragment,嵌套Fragment中使用ToolBar
- 【android】在fragment中使用option menu
- 在Fragment中使用Loader机制介绍
- ViewPagerInFragment 在Fragment中使用ViewPager
- 在Fragment中使用百度地图
- 在ViewPager中使用Fragment嵌套问题
- 如何在Fragment中使用findViewById呢?
- 在Fragment中使用Gridview+Spinner
- DrawerLayout不能放在fragment中使用
- 导航栏透明 毛玻璃效果iOS
- javascript事件执行顺序
- 动态时间规整(Dynamic Time Warping)
- Spark LDA
- 自用cocoapods命令整合
- Fragment在xml中使用
- 使用Criteria确定android location
- 基于 Jenkins 快速搭建持续集成环境
- 手机端拨号连接短信连接 qq在线客服代码
- 简单工厂模式
- 文章标题
- C++之动态配置ip信息
- JS实现文本框联动
- Java Web学习总结(22)——使用kaptcha生成验证码