- 浏览: 438152 次
文章分类
最新评论
-
barryzhong:
这篇文章挺不错。令人汗颜的是,那可是微软06年的文章。现在都快 ...
推荐一篇关于多租户Multi-Tenant数据架构的文章 -
Mybeautiful:
设计模式只是一个思路或是方案,碰到某种问题是有什么办法比较好的 ...
再见了模式
Android程序手机平板两不误:Fragment轻松实现
此篇文章系转载自:http://mobile.51cto.com/aprogram-396838.htm
是觉得这个写的不错,目前没有时间,没进行太详细的去研究,留着有时间好好研究,也分享给需要的朋友。如过作者对转载有看法,请直接联系我。ps:文章不是我的,里面的那些人物事件,以及工作经历系都是原作者的。非常感谢他的分享
记得我之前参与开发过一个华为的项目,要求程序可以支持好几种终端设备,其中就包括 Android手机和AndroidPad。然后为了节省人力,公司无节操地让Android手机和AndroidPad都由我们团队开发。当时项目 组定的方案是,制作两个版本的App,一个手机版,一个Pad版。由于当时手机版的主体功能已经做的差不多了,所以Pad版基本上就是把手机版的代码完全 拷过来,然后再根据平板的特性部分稍作修改就好了。
但是,从此以后我们就非常苦 逼了。每次要添加什么新功能,同样的代码要写两遍。每次要修复任何bug,都要在手机版代码和Pad版代码里各修改一遍。这还不算什么,每到出版本的时候 就更离谱了。华为要求每次需要出两个版本,一个华为内网环境的版本,一个客户现场的版本,而现在又分了手机和Pad,也就是每次需要出四个版本。如果在出 完版本后自测还出现了问题,就可以直接通宵了。这尤其是苦了我们的X总(由于他dota打的比较好,我都喜欢叫他X神)。他在我们项目组中单独维护一个模 块,并且每次打版本都是由他负责,加班的时候我们都能跑,就是他跑不了。这里也是赞扬一下我们X神的敬业精神,如果他看得到的话。
经历过那么苦逼时期的我也就开始思考,可不可以制作同时兼容手机和平板的App呢?答案当然是肯定的,不过我这个人比较懒,一直也提不起精神去钻研这个问题。直到我一个在美国留学的朋友Gong让我帮她解决她的研究生导师布置的作业(我知道你研究生导师看不懂中文^-^),正好涉及到了这一块,也就借此机会研究了一下,现在拿出来跟大家分享。
我们先来看一下Android手机的设置界面,点击一下Sound,可以跳转到声音设置界面,如下面两张图所示:
然后再来看一下AndroidPad的设置界面,主设置页面和声音设置页面都是在一个界面显示的,如下图所示:
如果这分别是两个不同的App做出的效果,那没有丝毫惊奇之处。但如果是同一个App,在手机上和平板上运行分别有以上两种效果的话,你是不是就已经心动了?我们现在就来模拟实现一下。
首先你需要对Fragment有一定的了解,如果你还没接触过Fragment,建议可以先阅读AndroidFragment完全解析,关于碎片你所需知道的一切这篇文章。并且本次的代码是运行在Android4.0版本上的,如果你的SDK版本还比较低的话,建议可以先升升级了。
新建一个Android项目,取名叫FragmentDemo。打开或新建MainActivity作为程序的主Activity,里面有如下自动生成的内容:
- publicclassMainActivityextendsActivity{
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- }
- publicclassMainActivityextendsActivity{
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- }
作为一个Android老手,上面的代码实在太小儿科了,每个Activity中都会有这样的代码。不过今天我们的程序可不会这么简单,加载布局这一块还是大有文章的。
打开或新建res/layout/activity_main.xml作为程序的主布局文件,里面代码如下:
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal"
- tools:context=".MainActivity">
- <fragment
- android:id="@+id/menu_fragment"
- android:name="com.example.fragmentdemo.MenuFragment"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </LinearLayout>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal"
- tools:context=".MainActivity">
- <fragment
- android:id="@+id/menu_fragment"
- android:name="com.example.fragmentdemo.MenuFragment"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </LinearLayout>
这个布局引用了一个MenuFragment,我们稍后来进行实现,先来看一下今天的一个重点,我们需要再新建一个activity_main.xml,这个布局文件名和前面的主布局文件名是一样的,但是要放在不同的目录下面。
在res目录下新建layout-large目录,然后这个目录下创建新的activity_main.xml,加入如下代码:
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal"
- android:baselineAligned="false"
- tools:context=".MainActivity"
- >
- <fragment
- android:id="@+id/left_fragment"
- android:name="com.example.fragmentdemo.MenuFragment"
- android:layout_width="0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- <FrameLayout
- android:id="@+id/details_layout"
- android:layout_width="0dip"
- android:layout_height="fill_parent"
- android:layout_weight="3"
- ></FrameLayout>
- </LinearLayout>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal"
- android:baselineAligned="false"
- tools:context=".MainActivity"
- >
- <fragment
- android:id="@+id/left_fragment"
- android:name="com.example.fragmentdemo.MenuFragment"
- android:layout_width="0dip"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- <FrameLayout
- android:id="@+id/details_layout"
- android:layout_width="0dip"
- android:layout_height="fill_parent"
- android:layout_weight="3"
- ></FrameLayout>
- </LinearLayout>
这个布局同样也引用了MenuFragment,另外还加入了一个FrameLayout用于显示详细内容。其实也就是分别对应了平板界面上的左侧布局和右侧布局。
这里用到了动态加载布局的技巧,首先Activity中调用setContentView(R.layout.activity_main),表明当前 的Activity想加载activity_main这个布局文件。而Android系统又会根据当前的运行环境判断程序是否运行在大屏幕设备上,如果运 行在大屏幕设备上,就加载layout-large目录下的activity_main.xml,否则就默认加载layout目录下的 activity_main.xml。
关于动态加载布局的更多内容,可以阅读Android官方提供的支持不同屏幕大小的全部方法这篇文章。
下面我们来实现久违的MenuFragment,新建一个MenuFragment类继承自Fragment,具体代码如下:
- publicclassMenuFragmentextendsFragmentimplementsOnItemClickListener{
- /**
- *菜单界面中只包含了一个ListView。
- */
- privateListViewmenuList;
- /**
- *ListView的适配器。
- */
- privateArrayAdapter<String>adapter;
- /**
- *用于填充ListView的数据,这里就简单只用了两条数据。
- */
- privateString[]menuItems={"Sound","Display"};
- /**
- *是否是双页模式。如果一个Activity中包含了两个Fragment,就是双页模式。
- */
- privatebooleanisTwoPane;
- /**
- *当Activity和Fragment建立关联时,初始化适配器中的数据。
- */
- @Override
- publicvoidonAttach(Activityactivity){
- super.onAttach(activity);
- adapter=newArrayAdapter<String>(activity,android.R.layout.simple_list_item_1,menuItems);
- }
- /**
- *加载menu_fragment布局文件,为ListView绑定了适配器,并设置了监听事件。
- */
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
- Viewview=inflater.inflate(R.layout.menu_fragment,container,false);
- menuList=(ListView)view.findViewById(R.id.menu_list);
- menuList.setAdapter(adapter);
- menuList.setOnItemClickListener(this);
- returnview;
- }
- /**
- *当Activity创建完毕后,尝试获取一下布局文件中是否有details_layout这个元素,如果有说明当前
- *是双页模式,如果没有说明当前是单页模式。
- */
- @Override
- publicvoidonActivityCreated(BundlesavedInstanceState){
- super.onActivityCreated(savedInstanceState);
- if(getActivity().findViewById(R.id.details_layout)!=null){
- isTwoPane=true;
- }else{
- isTwoPane=false;
- }
- }
- /**
- *处理ListView的点击事件,会根据当前是否是双页模式进行判断。如果是双页模式,则会动态添加Fragment。
- *如果不是双页模式,则会打开新的Activity。
- */
- @Override
- publicvoidonItemClick(AdapterView<?>arg0,Viewview,intindex,longarg3){
- if(isTwoPane){
- Fragmentfragment=null;
- if(index==0){
- fragment=newSoundFragment();
- }elseif(index==1){
- fragment=newDisplayFragment();
- }
- getFragmentManager().beginTransaction().replace(R.id.details_layout,fragment).commit();
- }else{
- Intentintent=null;
- if(index==0){
- intent=newIntent(getActivity(),SoundActivity.class);
- }elseif(index==1){
- intent=newIntent(getActivity(),DisplayActivity.class);
- }
- startActivity(intent);
- }
- }
- }
- publicclassMenuFragmentextendsFragmentimplementsOnItemClickListener{
- /**
- *菜单界面中只包含了一个ListView。
- */
- privateListViewmenuList;
- /**
- *ListView的适配器。
- */
- privateArrayAdapter<String>adapter;
- /**
- *用于填充ListView的数据,这里就简单只用了两条数据。
- */
- privateString[]menuItems={"Sound","Display"};
- /**
- *是否是双页模式。如果一个Activity中包含了两个Fragment,就是双页模式。
- */
- privatebooleanisTwoPane;
- /**
- *当Activity和Fragment建立关联时,初始化适配器中的数据。
- */
- @Override
- publicvoidonAttach(Activityactivity){
- super.onAttach(activity);
- adapter=newArrayAdapter<String>(activity,android.R.layout.simple_list_item_1,menuItems);
- }
- /**
- *加载menu_fragment布局文件,为ListView绑定了适配器,并设置了监听事件。
- */
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
- Viewview=inflater.inflate(R.layout.menu_fragment,container,false);
- menuList=(ListView)view.findViewById(R.id.menu_list);
- menuList.setAdapter(adapter);
- menuList.setOnItemClickListener(this);
- returnview;
- }
- /**
- *当Activity创建完毕后,尝试获取一下布局文件中是否有details_layout这个元素,如果有说明当前
- *是双页模式,如果没有说明当前是单页模式。
- */
- @Override
- publicvoidonActivityCreated(BundlesavedInstanceState){
- super.onActivityCreated(savedInstanceState);
- if(getActivity().findViewById(R.id.details_layout)!=null){
- isTwoPane=true;
- }else{
- isTwoPane=false;
- }
- }
- /**
- *处理ListView的点击事件,会根据当前是否是双页模式进行判断。如果是双页模式,则会动态添加Fragment。
- *如果不是双页模式,则会打开新的Activity。
- */
- @Override
- publicvoidonItemClick(AdapterView<?>arg0,Viewview,intindex,longarg3){
- if(isTwoPane){
- Fragmentfragment=null;
- if(index==0){
- fragment=newSoundFragment();
- }elseif(index==1){
- fragment=newDisplayFragment();
- }
- getFragmentManager().beginTransaction().replace(R.id.details_layout,fragment).commit();
- }else{
- Intentintent=null;
- if(index==0){
- intent=newIntent(getActivity(),SoundActivity.class);
- }elseif(index==1){
- intent=newIntent(getActivity(),DisplayActivity.class);
- }
- startActivity(intent);
- }
- }
- }
这个类的代码并不长,我简单的说明一下。在onCreateView方法中加载了menu_fragment这个布局,这个布局里面包含 了一个ListView,然后我们对这个ListView填充了两个简单的数据"Sound"和"Display"。又在 onActivityCreated方法中做了一个判断,如果Activity的布局中包含了details_layout这个元素,那么当前就是双页模 式,否则就是单页模式。onItemClick方法则处理了ListView的点击事件,发现如果当前是双页模式,就动态往details_layout 中添加Fragment,如果当前是单页模式,就直接打开新的Activity。
我们把MenuFragment中引用到的其它内容一个个添加进来。新建menu_fragment.xml文件,加入如下代码:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ListView
- android:id="@+id/menu_list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- ></ListView>
- </LinearLayout>
- <?xmlversion="1.0"encoding="UTF-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ListView
- android:id="@+id/menu_list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- ></ListView>
- </LinearLayout>
然后新建SoundFragment,里面内容非常简单:
- publicclassSoundFragmentextendsFragment{
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
- Viewview=inflater.inflate(R.layout.sound_fragment,container,false);
- returnview;
- }
- }
- publicclassSoundFragmentextendsFragment{
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
- Viewview=inflater.inflate(R.layout.sound_fragment,container,false);
- returnview;
- }
- }
这里SoundFragment需要用到sound_fragment.xml布局文件,因此这里我们新建这个布局文件,并加入如下代码:
- <?xmlversionxmlversion="1.0"encoding="utf-8"?>
- ativeLayoutxmlns:androidRelativeLayoutxmlns: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_centerInParent="true"
- android:textSize="28sp"
- android:textColor="#000000"
- android:text="Thisissoundview"
- />
- </RelativeLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns: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_centerInParent="true"
- android:textSize="28sp"
- android:textColor="#000000"
- android:text="Thisissoundview"
- />
- </RelativeLayout>
同样的道理,我们再新建DisplayFragment和display_fragment.xml布局文件:
- publicclassDisplayFragmentextendsFragment{
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){
- Viewview=inflater.inflate(R.layout.display_fragment,container,false);
- returnview;
- }
- }
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#0000ff"
- android:orientation="vertical">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:textSize="28sp"
- android:textColor="#000000"
- android:text="Thisisdisplayview"
- />
- </RelativeLayout>
- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#0000ff"
- android:orientation="vertical">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:textSize="28sp"
- android:textColor="#000000"
- android:text="Thisisdisplayview"
- />
- </RelativeLayout>
然后新建SoundActivity,代码如下:
- publicclassSoundActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.sound_activity);
- }
- }
- publicclassSoundActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.sound_activity);
- }
- }
这个Activity只是加载了一个布局文件,现在我们来实现sound_activity.xml这个布局文件:
- <?xmlversion="1.0"encoding="utf-8"?>
- <fragmentxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/sound_fragment"
- android:name="com.example.fragmentdemo.SoundFragment"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </fragment>
- <?xmlversion="1.0"encoding="utf-8"?>
- <fragmentxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/sound_fragment"
- android:name="com.example.fragmentdemo.SoundFragment"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </fragment>
这个布局文件引用了SoundFragment,这样写的好处就是,以后我们只需要在SoundFragment中修改代码,SoundActivity就会跟着自动改变了,因为它所有的代码都是从SoundFragment中引用过来的。
好,同样的方法,我们再完成DisplayActivity:
- publicclassDisplayActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.display_activity);
- }
- }
- publicclassDisplayActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.display_activity);
- }
- }
然后加入display_activity.xml:
- <?xmlversion="1.0"encoding="utf-8"?>
- <fragmentxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/display_fragment"
- android:name="com.example.fragmentdemo.DisplayFragment"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </fragment>
- <?xmlversion="1.0"encoding="utf-8"?>
- <fragmentxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/display_fragment"
- android:name="com.example.fragmentdemo.DisplayFragment"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- </fragment>
现在所有的代码就都已经完成了,我们来看一下效果吧。
首先将程序运行在手机上,效果图如下:
分别点击Sound和Display,界面会跳转到声音和显示界面:
然后将程序在平板上运行,点击Sound,效果图如下:
然后点击Display切换到显示界面,效果图如下:
这样我们就成功地让程序同时兼容手机和平板了。当然,这只是一个简单的demo,更多复杂的内容需要大家自己去实现了。
好了,今天的讲解到此结束。
相关推荐
Android使用Fragment实现兼容手机和平板的程序
Android 程序技术 本节课程内容:Fragment的介绍 Fragment Fragment(碎片)是一种可以嵌入在Activity中的UI片段,与Activity非常相似,不仅包含布局,同时也具有自己的生命周期 Fragment不能独立存在必须嵌入到...
androidx-fragment-1.1.0.aar
此为示例代码,详细讲解请参考 http://blog.csdn.net/sinyu890807/article/details/8744943
疯兄认为,一个设计优良的android应用程序,必定得益于Fragment的完美使用。本集主要内容如下:1. Fragment简介 2. Fragment的创建和实现 3. Fragment和Activity的数据传递 4. Fragment管理器和事务
- Fragment是一种可以嵌入在Activity当中的UI片段,它能让程序更加合理和充分地利用大屏幕的空间,因而在平板上应用得非常广泛。 - 和Activity很像,同样都能包含布局,同样都有自己的生命周期。 - 手机平板要兼顾,...
android兼容手机和平板的Fragment
这是Pro Android学习Fragment部分的例子源代码。
Fragment的概念是从Android3.0开始引入的,直译为碎片、片段,目的是为不同屏幕大小的设备(手机、平板等)创建灵活动态的UI。诚如其名,你可以把Fragment当作是Activity的模块化组件,它拥有自己的生命周期和UI,接受...
以前做这种新闻标签页都是用...当把api升级到最新后发现这个LocalActivityManager已经被废弃了,取而代之的是FragmentManager,经过一下午的研究终于用FragmentManager实现了新闻滑动标签页的效果。
android:Fragment和Activity之间的通讯分享.pdf
问题解决:Fragment not attached to Activity的相关代码,更多详细内容请参考:http://blog.csdn.net/u012939909/article/details/53355836
通过一个小程序实现了Android的Fragment,有兴趣的可以下载看看
android 二维码扫描页面Fragment 的实现,使用非常简单,把项目导入自己的studio中, MipcaFragmentCapture mipcaFragmentCapture = new MipcaFragmentCapture();mipcaFragmentCapture.setIndicator(indicator); ...
主要给大家介绍了Android在Fragment中实现监听触摸事件的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
android平板电脑开发,使用Fragment实现页面的局部跳转,像管理系统一样,点击左边的连接实现右边页面的跳转
Android程序研发源码Android ViewPager+Fragment实现QQ界面
在android 2.x时代,我们可能会地使用ActivityGroup来实现这种,但是随着jelly bean的市场份额超过50%,我们会发现有一种新的组建出现了,它叫Fragment...而这种底部选项卡的风格界面的实现也由ActivityGroup转向了...
通过不同尺寸的屏幕来识别手机与平板,根据当前布局中是否有专门为平板布局的控件ID来区分手机与平板,从而实现手机与平板不不同的样式和功能
我的微博《页面架构二:Fragment+LinearLayout》对应的完整源代码的module