Android fragment入门二
来源:互联网 发布:软件测试项目 编辑:程序博客网 时间:2024/05/29 16:36
fragment不兼容3.0以下的版本,所有开发时需要使用Android扩展的兼容jar包,引入其中的android.support.v4.app.FragmentActivity;android.support.v4.app.Fragment;
下面做了一个fragment相互通讯的例子:
package com.example.fragment;import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
package com.example.fragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;public class Fragment1 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment1, null);Button bt = (Button)view.findViewById(R.id.bt);bt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//getSupportFragmentManager 是导入的 Android v4包兼容3.0以下版本//首先得到此Fragment的父容器activity,在父容器获取里面其他的子FragmentFragment2 fragment2 = (Fragment2) getActivity().getSupportFragmentManager().findFragmentById(R.id.f2);fragment2.setText("fragment2的文本被改变了");}});return view;}}
package com.example.fragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class Fragment2 extends Fragment{TextView tv;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {//填充器,将fragment的xml文件,实例化成对象View view = inflater.inflate(R.layout.fragment2, null);tv = (TextView) view.findViewById(R.id.tv);return view;}public void setText(String s) {// TODO Auto-generated method stubtv.setText(s);}}activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <fragment android:id="@+id/f1" android:name="com.example.fragment.Fragment1" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" /> <fragment android:id="@+id/f2" android:name="com.example.fragment.Fragment2" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" /></LinearLayout>fragment1.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:background="#ff0000" > <Button android:id="@+id/bt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="改变fragment2" /></RelativeLayout>fragment2.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:background="#ffff00" > <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" android:text="fragment2" /></RelativeLayout>
0 0
- Android fragment入门二
- Android之Fragment入门二(动态添加碎片)
- Android入门——Fragment详解之Fragment与Activity通信及数据交换(二)
- Android-Fragment入门用法
- Android fragment入门一
- Android入门之Fragment
- android Fragment入门
- 【Android】Fragment入门
- android Fragment 入门介绍
- android Fragment 入门
- Android Fragment 入门介绍
- Android入门之Fragment
- Android Fragment实践(二)
- 40 Android fragment (二)
- Android fragment (二)
- Android Fragment(二)---使用
- android fragment 详解(二)
- Android之Fragment(二)
- 基础只是要了解
- MySQL批量SQL插入性能优化
- locate和updatedb
- 使用Jenkins+Calabash+Cocoapods搭建iOS持续集成环境
- 【LeetCode】Clone Graph
- Android fragment入门二
- hdu 2222
- 【C语言】08-数组
- 统计学习笔记(1)——统计学习方法概论
- 控制由Run运行的程序(简称Run程序)
- 数据仓库的深入理解
- zfs_arc_max设置以后没有生效
- 502问题解决方案
- 统计学习笔记(2)——感知机模型