<LinearLayout 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:orientation="horizontal"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fg1"
android:name="com.example.fragmentdemo1.Fragment1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
</LinearLayout>2.创建Fragment类:package com.example.fragmentdemo1;
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 Fragment1 extends Fragment
{
private TextView tv = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.f1,container);
tv = (TextView) view.findViewById(R.id.tv1);
return view;
}
public void setText(String text)
{
tv.setText(text);
}
}3.Fragment类对应的布局<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="文本1"
/>
</LinearLayout>
getActivity().getSupportFragmentManager().findFragmentById(R.id.fg1);
package com.example.fragmentdemo1;
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 Fragment1 extends Fragment
{
private TextView tv = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.f1,container);
tv = (TextView) view.findViewById(R.id.tv1);
return view;
}
public void setText(String text)
{
tv.setText(text);
}
}布局:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="文本1"
/>
</LinearLayout>fragment2:package com.example.fragmentdemo1;
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;
import android.widget.TextView;
public class Fragment2 extends Fragment
{
private Button but = null;
private TextView tv = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.f2, container);
but = (Button) view.findViewById(R.id.but);
tv = (TextView) view.findViewById(R.id.tv2);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
but.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Fragment1 f1 = (Fragment1) Fragment2.this.getActivity().getSupportFragmentManager().findFragmentById(R.id.fg1);
f1.setText("哈哈");
}
});
}
}布局:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00"
android:orientation="vertical" >
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本2"
/>
<Button
android:id="@+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置文本"
/>
</LinearLayout>
Mainactivity:package com.example.fragmentdemo1;
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);
}
}布局:<LinearLayout 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:orientation="horizontal"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fg1"
android:name="com.example.fragmentdemo1.Fragment1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
/>
<fragment
android:id="@+id/fg2"
android:name="com.example.fragmentdemo1.Fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Display display = getWindowManager().getDefaultDisplay();
if (display.getWidth() > display.getHeight()) {
Fragment1 fragment1 = new Fragment1();
ft.replace(android.R.id.content, fragment1).commit();
} else {
Fragment2 fragment2 = new Fragment2();
ft.replace(android.R.id.content, fragment2).commit();
} onDestroyView:Fragment中的布局被移除时调用
onDetach:Fragment和Activity解除关联的时候调用
大家可以重写所有生命周期方法,然后通过打log的方式观察生命周期,这里就不贴代码了。
原文:http://blog.csdn.net/chdjj/article/details/21525331