}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".fragment.Test1FragmentActivity">
<fragment
android:id="@+id/fragment_title"
android:layout_width=“match_parent”
android:layout_height=“50dp”
android:name=“com.gs.common3.fragment.TitleFragment”
/>
<fragment
android:id="@+id/fragment_content"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_below="@+id/fragment_title"
android:name=“com.gs.common3.fragment.ContentFragment”
/>
1.1.2、总结
1.2、Fragment动态应用
代码如下:
/**
-
演示Fragment的动态使用
-
-
案例效果:在Activity界面中有两个Fragment 标题和内容
*/
public class Test2FragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2_fragment);
//1.创建Fragment的管理器对象;
FragmentManager manager = getFragmentManager();
//2.获取Fragment的事务对象并开启事务;
FragmentTransaction transaction = manager.beginTransaction();
//3.调用事务中相应的动态操作Fragment的方法执行;
transaction.add(R.id.title_layout, new TitleFragment());
transaction.add(R.id.content_layout, new ContentFragment());
//4.提交事务;
transaction.commit();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".fragment.Test2FragmentActivity">
<LinearLayout
android:id="@+id/title_layout"
android:layout_width=“match_parent”
android:layout_height=“50dp”
android:orientation=“vertical”>
<LinearLayout
android:id="@+id/content_layout"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”>
2、Fragment动态切换
==============
ShopRankFragment
public class ShopRankFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceSt
ate) {
return inflater.inflate(R.layout.fragment_shop_rank,null);
}
}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”<