Android----Fragment的基本用法
2015-10-22 10:10
375 查看
Android是在Android 3.0 (API level 11)开始引入Fragment的。
可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。
可以把Fragment设计成可以在多个Activity中复用的模块。
fragment的实例:
实现功能:根据横竖屏显示不同的内容。
fragment1.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"
android:background="#aa0000ff"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="35sp"
android:text="横屏显示"/>
</LinearLayout>
fragment2.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"
android:background="#00ff00"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="35sp"
android:text="竖屏显示"/>
</LinearLayout>要显示fragment中的内容需要继承类Fragment,分别创建类Fragment1、Fragment2。
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
return view;
}
}
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int width = getWindowManager().getDefaultDisplay().getWidth();
int length = getWindowManager().getDefaultDisplay().getHeight();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (width < length) {
ft.replace(android.R.id.content, new Fragment2());
} else {
ft.replace(android.R.id.content, new Fragment1());
}
ft.commit();
}
}
需要注意的是:Fragment的使用需要开启事务,最后要提交。
<span style="color:#FF0000;">FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();</span>
<span style="color:#FF0000;">ft.commit();</span>
实现的效果:
另一个fragment的例子,实现的功能:分模块显示内容
创建4个模块要显示的内容:
<?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:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体育新闻" />
</LinearLayout>
布局文件
为了让点击时更容易识别到点击的效果,在drawable下添加以下文件font_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:color="#00aaff"/>
<item android:state_pressed="true" android:color="#33aaff"/>
<item android:color="#1177ff"/>
</selector>
activity_main.xml
<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="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/tab1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#aa1265"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="体育新闻" />
<TextView
android:id="@+id/tab2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#32a221"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="社会新闻" />
<TextView
android:id="@+id/tab3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#387221"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="国内新闻" />
<TextView
android:id="@+id/tab4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#321cd1"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="国际新闻" />
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
实现效果:
可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。
可以把Fragment设计成可以在多个Activity中复用的模块。
fragment的实例:
实现功能:根据横竖屏显示不同的内容。
fragment1.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"
android:background="#aa0000ff"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="35sp"
android:text="横屏显示"/>
</LinearLayout>
fragment2.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"
android:background="#00ff00"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="35sp"
android:text="竖屏显示"/>
</LinearLayout>要显示fragment中的内容需要继承类Fragment,分别创建类Fragment1、Fragment2。
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
return view;
}
}
public class Fragment2 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, null); } }
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int width = getWindowManager().getDefaultDisplay().getWidth();
int length = getWindowManager().getDefaultDisplay().getHeight();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (width < length) {
ft.replace(android.R.id.content, new Fragment2());
} else {
ft.replace(android.R.id.content, new Fragment1());
}
ft.commit();
}
}
需要注意的是:Fragment的使用需要开启事务,最后要提交。
<span style="color:#FF0000;">FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();</span>
<span style="color:#FF0000;">ft.commit();</span>
实现的效果:
另一个fragment的例子,实现的功能:分模块显示内容
创建4个模块要显示的内容:
<?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:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体育新闻" />
</LinearLayout>
<?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:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="社会新闻" /> </LinearLayout>
<?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:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="国内新闻" /> </LinearLayout>
<?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:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="国际新闻" /> </LinearLayout>
布局文件
为了让点击时更容易识别到点击的效果,在drawable下添加以下文件font_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:color="#00aaff"/>
<item android:state_pressed="true" android:color="#33aaff"/>
<item android:color="#1177ff"/>
</selector>
activity_main.xml
<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="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/tab1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#aa1265"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="体育新闻" />
<TextView
android:id="@+id/tab2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#32a221"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="社会新闻" />
<TextView
android:id="@+id/tab3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#387221"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="国内新闻" />
<TextView
android:id="@+id/tab4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#321cd1"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="国际新闻" />
</LinearLayout>
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
实现效果:
相关文章推荐
- Android Fragment 基本了解(图文介绍)
- Android Fragment的使用方法(翻译)
- Android 中 Fragment的使用大全
- Android基础之使用Fragment适应不同屏幕和分辨率(分享)
- Android中给fragment写入参数的轻量开发包FragmentArgs简介
- Android Fragment中使用SurfaceView切换时闪一下黑屏的解决办法
- Fragment里添加ListView不要用ListFragment
- 详解Android开发中Fragment的使用
- Android中fragment嵌套fragment问题解决方法
- Android基础之Fragment与Activity交互详解
- Android基础之使用Fragment控制切换多个页面
- Fragment
- Android Fragment 真正的完全解析(下)
- Android之Fragment
- Fragment+viewpager+Fragment 嵌套 第二次进入该fragment不显示数据
- android fragment详细介绍
- Android4.0-Fragment框架实现方式剖析
- Fragment-FragmentMannager中的方法
- Fragment与Activity通讯
- 理解Fragment生命周期