Android的1.x和2.x适用于手机,3.x适用于平板,而从4.0开始整合了手机与平板的系统,让4.x的系统适用于手机与平板。
java
一:Fragment
android
Fragment是Android的一个重要的特性,从字面意思理解为“碎片”,实质上就是可让Activity有多个显示区域来显示内容,他有本身的生命周期,可是他的生命周期依赖于他所附属的Activity的生命周期。
app
1:生命周期
ide
经过上面的图片咱们可总结出以下的几条规律:
布局
(1)Activity的生命周期将直接影响Fragment的生命周期
this
(2)Fragment实质上是将Activity划分红了几个部分
spa
(3)Fragment能够处于Activity状态与之对应的状态及如下状态。如:Activity处于stopped状态时,Fragment能够处于onStop、onDestoryView及如下状态。
设计
2:管理与操做Fragment
xml
(1)管理Fragment
对象
经过getFragmentManager得到管理对象,主要方法有
findFragmentById(int id):经过id查找到Fragment
findFragmentByTag(String tag):经过Tag找到Fragment
(2)操做Fragment
经过FragmentManager对象的getTranscation得到操做对象
add(int containerViewID,Fragment fragment,String tag):向Activity中添加一个Fragment对象
show(Fragment fragment):显示已经隐藏的Fragment
remove(Fragment fragment):移除已经存在的Fragment
hide(Fragment fragment):隐藏显示的Fragment
commit():注册这个Transcation
二:ActionBar
在2.x版本中的实现导航样式的TabHost,在4.x的版本中对齐又进行了美化。原来用2.x开发的导航能够运行在4.x版本的设备上,可是4.x开发的导航却没法运行在2.x版本的设备上。
用途之一:作导航标签
使用ActionBar设计的导航能够自适应屏幕的分辨率,能够避免适配的问题。具体实现主要由如下几个步骤
(1)实现ActionBar.TabListener接口,重写其中的几个方法以响应用户切换导航标签的操做。也就是说,一个标签注册一个监听,添加一个Fragment到Activity上,完成一个操做。
(2)实例化ActionBar.Tab对象,一个对象就是一个标签,能够调用set系列方法设置标签的样式及操做等
(3)调用addTab()将标签添加到导航ActionBar中。
三:结合两大特性制做导航
这里想达到的效果是:在一个Activity上添加一个导航,随着单击导航标签,在同一个Activity上的不一样区域上切换显示内容。
1:界面布局
主界面就是一个LinearLayout,且给这个布局一个id;另外的2个界面,随意添加什么控件都可以(这里我添加的一个是TextVIew一个是ImageView)。
主界面布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".MainActivity" > </LinearLayout> |
布局1:textfragment.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:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> |
布局2:p_w_picpathfragment.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:orientation="vertical" > <ImageView android:id="@+id/p_w_picpathView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> </LinearLayout> |
2:Fragment类
有多少个转换的界面就有多少个继承自Fragment的类,重写onCreateView方法,加载对应界面要显示的xml文件,得到界面上的视图View
区域1:TextFragment.java
//继承自Fragment public class TextFragment extends Fragment{ private View view;//声明视图 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加载对应界面的布局,得到视图View view = inflater.inflate(R.layout.textfragment, container,false); //返回View return view; } } |
区域2:ImageFragment.java
//继承自Fragment public class ImageFragment extends Fragment{ //声明视图 private View p_w_picpathview; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加载布局,返回视图 p_w_picpathview = inflater.inflate(R.layout.p_w_picpathfragment, container, false); return p_w_picpathview; } } |
3:主界面
建立导航,导航标签对象,设置导航标签的监听以响应操做等,添加标签到导航上。
public class MainActivity extends Activity { // 声明导航对象 private ActionBar actionBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 加载视图 setContentView(R.layout.activity_main); // 实例化导航对象 actionBar = getActionBar(); // 设置导航的显示样式 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // 建立第一个区域 TextFragment textFragment = new TextFragment(); // 实例化第一个标签对象 Tab tab1 = actionBar.newTab(); tab1.setIcon(R.drawable.ic_launcher);// 设置标签的图标样式 tab1.setText("选项卡1");// 设置图标显示的文字 tab1.setTabListener(new TextListener(textFragment));// 给标签添加相应的监听 actionBar.addTab(tab1);// 添加标签到导航上 // 建立第二个区域 ImageFragment p_w_picpathFragment = new ImageFragment(); // 建立第二个标签 Tab tab2 = actionBar.newTab(); tab2.setIcon(R.drawable.ic_launcher);// 设置标签的图标样式 tab2.setText("选项卡2");// 设置标签的文字 tab2.setTabListener(new IamgeListener(p_w_picpathFragment));// 给标签添加相应的监听 actionBar.addTab(tab2);// 添加标签到导航上 } // 内部类形式 实现监听第一个区域的类 class TextListener implements android.app.ActionBar.TabListener { private TextFragment textFragment;// 声明区域 // 构造方法 public TextListener(TextFragment textFragment) { this.textFragment = textFragment; } // 重写的三个方法 // 当标签从新选中时触发的方法 @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } // 当标签选中时触发的方法 @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.layout, textFragment, null);// 添加区域到Activity上的布局上 } // 当标签取消选中时触发的方法 @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(textFragment);// 移除区域 } } // 内部类形式 实现监听第二个区域的类 class IamgeListener implements android.app.ActionBar.TabListener { private ImageFragment p_w_picpathFragment;// 声明区域 // 构造方法 public IamgeListener(ImageFragment p_w_picpathFragment) { this.p_w_picpathFragment = p_w_picpathFragment; } // 当标签从新选中时触发的方法 @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { } // 当标签选中时触发的方法 @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.layout, p_w_picpathFragment, null);// 添加区域到Activity上的布局上 } // 当标签取消选中时触发的方法 @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(p_w_picpathFragment);// 移除区域 } } } |
4:结果
和2.x版本中的导航相似,单击不一样的标签时,下方会显示出不一样的界面。