a:静态添加
1)、创建Fragment继承V4包下的Fragment,添加布局创建视图
2)、在Activity的布局中使用fragment标签添加Fragment
class 或者 android:name
3)、修改Activity继承V4包下的FragmentActivity
b:动态添加
1)、创建Fragment继承V4包下的Fragment,添加布局创建视图
2)、修改Activity继承V4包下的FragmentActivity
3)、在Activity中,获得V4包下的FragmentManager
getSupportFragmentManager
静态添加
main_xml布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/ft_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.example.day12_fragment01.LeftFragment"
/>
<fragment
android:id="@+id/ft_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.day12_fragment01.RightFragment"
/>
左边framgment布局
<?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:orientation="vertical"
android:background="#ff0">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我在左边"
/>
</LinearLayout>
右边的fagment
<?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:orientation="vertical"
android:background="#f00"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我在右边"
/>
</LinearLayout>
代码main
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.left, null);
}
}
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.right, null);
}
}
动态添加
main_xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/ll_left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#f00"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/ll_right"
android:layout_width="0dp"
android:layout_weight="2"
android:background="#0f0"
android:layout_height="match_parent"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
left_fragment
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class MainActivity extends Activity {
@SuppressLint("CommitTransaction")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 碎片管理器,用来Fragment和Activity之间交互的一个接口
FragmentManager manager = getFragmentManager();
// 开启Fragment事务,事务提供三个可用方法, 添加(add),移除(remove),替换(replace)
FragmentTransaction transaction = manager.beginTransaction();
// 将Fragment添加到指定容器(指定的位置)
transaction.add(R.id.ll_left, new LeftFragment());
transaction.add(R.id.ll_right, new RightFragment());
// 提交事务,保存
transaction.commit();
}
}
—–LeftFragment
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.left, null);
}
}
------RightFragment
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.right, null);
}
}