Android fragment入门二
2014-05-08 13:30
411 查看
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
下面做了一个fragment相互通讯的例子:
package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected 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{ @Override public 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() { @Override public void onClick(View v) { //getSupportFragmentManager 是导入的 Android v4包兼容3.0以下版本 //首先得到此Fragment的父容器activity,在父容器获取里面其他的子Fragment Fragment2 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; @Override public 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 stub tv.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>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- 解決Linux下Android开发真机调试设备不被识别问题
- [Android]在代码里运行另一个程序的方法
- Android多窗口的实现