Fragment的2中载入方式!
1、静态有动态
代码如下:
public class MainActivity extends AppCompatActivity {
private ContentFragment cf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setFragment();
}
//动态载入
public void setFragment(){
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
cf = new ContentFragment();
ft.replace(R.id.fragment_content,cf);
ft.commit();
}
}
静态载入fragment布局
title_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="@string/title"/> </LinearLayout>
动态载入的布局:
context_layout.xml
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="@string/context"/>
</LinearLayout>
activity_main.xml布局
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lesson.hs.fragmentExp.MainActivity">
<!-- 自己定义的fragment-->
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fragment_title"
android:name="com.lesson.hs.fragmentExp.TitleFragment"
/>
<!-- 动态载入的fragment容器-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/fragment_title"
android:id="@+id/fragment_content"
/>
</RelativeLayout>
动态载入的fragment类:
public class ContentFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.context_layout,container,false);
return view;
}
}
静态载入的fragment类:
public class TitleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.title_layout,container,false);
return view;
}
}
Fragment的2中载入方式!的更多相关文章
- 【Android进阶篇】Fragment的两种载入方式
一.概述 Fragment(碎片,片段)是在Android 3.0后才引入的,基本的目的是为了实如今大屏幕设备上的更加动态更加灵活的UI设计. 这是由于平板电脑的屏幕比手机大得多,所以屏幕上能够放很多 ...
- Fragment在Activity中的应用 (转载)
原文链接 http://www.cnblogs.com/nanxin/archive/2013/01/24/2875341.html 在本小节中介绍在Activity中创建Fragment. 官网有很 ...
- FragmentActivity + Fragment + Fragment使用过程中出现的bug
FragmentActivity + Fragment(通过hide和show来显示fragment) + Fragment(通过viewpager来显示fragment) 在Activity中 // ...
- 清空Fragment回退栈中某个Fragment
出栈的方法:getActivity().getSupportFragmentManager().popBackStackImmediate(CreateOneFragment.class.getNam ...
- 解决Fragment在Viepager中切换不保存状态的问题
在FragmentPagerAdapter中重写以下方法: @Override public Object instantiateItem(ViewGroup container, int posit ...
- Fragment在xml中但作用不是显示view
2013-12-17 有时候会发现在xml文件中有使用fragment,但是却不是为了显示View,代码如下: <FrameLayout xmlns:android="http://s ...
- 【原创】【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析
ViewPager中切换界面Fragment被销毁的问题分析 1.使用场景 ViewPager+Fragment实现界面切换,界面数量>=3 2.Fragment生命周期以及与Activ ...
- 转载【ViewPager+Fragment】ViewPager中切换界面Fragment被销毁的问题分析
ViewPager中切换界面Fragment被销毁的问题分析 原文链接 http://www.cnblogs.com/monodin/p/3866441.html 1.使用场景 ViewPager+ ...
- Web Fragment在项目中的使用
Web Fragment 是什么 - 它是在 servlet 3.0开始支持的,可以把一个dy web项目拆分为多个项目,解耦合,使其在项目中开发效率提高,下面我演示简单的项目创建过程 用eclips ...
随机推荐
- mean函数
求平均值 th> a=torch.zeros(,) [.0002s] th> a [torch.DoubleTensor of size 1x3] [.0003s] th> a[{, ...
- Educational Codeforces Round 16 C
Description Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column ...
- H5网站借鉴
http://www.rdinfo.com.cn/index.shtml http://www.winployee.com/ http://www.lkkdesign.com/ http://www. ...
- js对数组排序
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- FZU 2151 OOXX Game
OOXX Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- factory工厂模式之抽象工厂AbstractFactory
* 抽象工厂: 意图在于创建一系列互相关联或互相依赖的对象. * 每个工厂都会创建一个或多个一系列的产品 * 适用于:产品不会变动,开始时所有产品都创建好,然后根据分类获取想要的 某一类产品(很像sp ...
- Android——android相对布局(RelativeLayout)及属性
RelativeLayout布局 android:layout_marginTop="25dip" //顶部距离 android:gravity="left" ...
- implement "slam_karto" package on a Freight robot
1. login ssh fetch@<robot ip or robot name> 2. set robot master modify .bashrc in robot's com ...
- Sbt的使用初步和用sbt插件生成eclipse工程
以前一直是用maven去管理java项目,现在开始写scala项目了但是在scala-ide中去编译scala项目和sbt的区别一直没弄清楚受到文章:http://my.oschina.net/yjw ...
- 移动端 移动web屏幕适配方案 随不同宽度的屏幕而改变
链接地址1:http://www.cnblogs.com/zjzhome/p/4802157.html 链接地址2:http://www.html-js.com/article/Mobile-term ...