Android使用Fragment實(shí)現(xiàn)兼容手機(jī)和平板的程序
一
記得我之前參與開(kāi)發(fā)過(guò)一個(gè)華為的項(xiàng)目,要求程序可以支持好幾種終端設(shè)備,其中就包括 Android 手機(jī)和 Android Pad。然后為了節(jié)省人力,公司無(wú)節(jié)操地讓 Android 手機(jī)和 Android Pad 都由我們團(tuán)隊(duì)開(kāi)發(fā)。當(dāng)時(shí)項(xiàng)目組定的方案是,制作兩個(gè)版本的 App,一個(gè)手機(jī)版,一個(gè) Pad 版。由于當(dāng)時(shí)手機(jī)版的主體功能已經(jīng)做的差不多了,所以 Pad 版基本上就是把手機(jī)版的代碼完全拷過(guò)來(lái),然后再根據(jù)平板的特性部分稍作修改就好了。
但是,從此以后我們就非常苦逼了。每次要添加什么新功能,同樣的代碼要寫(xiě)兩遍。每次要修復(fù)任何 bug,都要在手機(jī)版代碼和 Pad 版代碼里各修改一遍。這還不算什么,每到出版本的時(shí)候就更離譜了。華為要求每次需要出兩個(gè)版本,一個(gè)華為內(nèi)網(wǎng)環(huán)境的版本,一個(gè)客戶現(xiàn)場(chǎng)的版本,而現(xiàn)在又分了手機(jī)和 Pad,也就是每次需要出四個(gè)版本。如果在出完版本后自測(cè)還出現(xiàn)了問(wèn)題,就可以直接通宵了。這尤其是苦了我們的 X 總 (由于他 dota 打的比較好,我都喜歡叫他 X 神)。他在我們項(xiàng)目組中單獨(dú)維護(hù)一個(gè)模塊,并且每次打版本都是由他負(fù)責(zé),加班的時(shí)候我們都能跑,就是他跑不了。這里也是贊揚(yáng)一下我們 X 神的敬業(yè)精神,如果他看得到的話。
經(jīng)歷過(guò)那么苦逼時(shí)期的我也就開(kāi)始思考,可不可以制作同時(shí)兼容手機(jī)和平板的 App 呢?答案當(dāng)然是肯定的,不過(guò)我這個(gè)人比較懶,一直也提不起精神去鉆研這個(gè)問(wèn)題。直到我一個(gè)在美國(guó)留學(xué)的朋友 Gong 讓我?guī)退鉀Q她的研究生導(dǎo)師布置的作業(yè) (我知道你研究生導(dǎo)師看不懂中文 ^-^),正好涉及到了這一塊,也就借此機(jī)會(huì)研究了一下,現(xiàn)在拿出來(lái)跟大家分享。
二
我們先來(lái)看一下 Android 手機(jī)的設(shè)置界面,點(diǎn)擊一下 Sound,可以跳轉(zhuǎn)到聲音設(shè)置界面,如下面兩張圖所示:


然后再來(lái)看一下 Android Pad 的設(shè)置界面,主設(shè)置頁(yè)面和聲音設(shè)置頁(yè)面都是在一個(gè)界面顯示的,如下圖所示:

如果這分別是兩個(gè)不同的 App 做出的效果,那沒(méi)有絲毫驚奇之處。但如果是同一個(gè) App,在手機(jī)上和平板上運(yùn)行分別有以上兩種效果的話,你是不是就已經(jīng)心動(dòng)了?我們現(xiàn)在就來(lái)模擬實(shí)現(xiàn)一下。
首先你需要對(duì) Fragment 有一定的了解,如果你還沒(méi)接觸過(guò) Fragment,建議可以先閱讀 Android Fragment 完全解析,關(guān)于碎片你所需知道的一切 這篇文章。并且本次的代碼是運(yùn)行在 Android 4.0 版本上的,如果你的 SDK 版本還比較低的話,建議可以先升升級(jí)了。
新建一個(gè) Android 項(xiàng)目,取名叫 FragmentDemo。打開(kāi)或新建 MainActivity 作為程序的主 Activity,里面有如下自動(dòng)生成的內(nèi)容:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
作為一個(gè) Android 老手,上面的代碼實(shí)在太小兒科了,每個(gè) Activity 中都會(huì)有這樣的代碼。不過(guò)今天我們的程序可不會(huì)這么簡(jiǎn)單,加載布局這一塊還是大有文章的。
打開(kāi)或新建 res/layout/activity_main.xml 作為程序的主布局文件,里面代碼如下:
<LinearLayout xmlns: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>
這個(gè)布局引用了一個(gè) MenuFragment,我們稍后來(lái)進(jìn)行實(shí)現(xiàn),先來(lái)看一下今天的一個(gè)重點(diǎn),我們需要再新建一個(gè) activity_main.xml,這個(gè)布局文件名和前面的主布局文件名是一樣的,但是要放在不同的目錄下面。
在 res 目錄下新建 layout-large 目錄,然后這個(gè)目錄下創(chuàng)建新的 activity_main.xml,加入如下代碼:
<LinearLayout xmlns: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>
這個(gè)布局同樣也引用了 MenuFragment,另外還加入了一個(gè) FrameLayout 用于顯示詳細(xì)內(nèi)容。其實(shí)也就是分別對(duì)應(yīng)了平板界面上的左側(cè)布局和右側(cè)布局。
這里用到了動(dòng)態(tài)加載布局的技巧,首先 Activity 中調(diào)用 setContentView(R.layout.activity_main) ,表明當(dāng)前的 Activity 想加載 activity_main 這個(gè)布局文件。而 Android 系統(tǒng)又會(huì)根據(jù)當(dāng)前的運(yùn)行環(huán)境判斷程序是否運(yùn)行在大屏幕設(shè)備上,如果運(yùn)行在大屏幕設(shè)備上,就加載 layout-large 目錄下的 activity_main.xml,否則就默認(rèn)加載 layout 目錄下的 activity_main.xml。
關(guān)于動(dòng)態(tài)加載布局的更多內(nèi)容,可以閱讀 Android 官方提供的支持不同屏幕大小的全部方法 這篇文章。
三
下面我們來(lái)實(shí)現(xiàn)久違的 MenuFragment,新建一個(gè) MenuFragment 類繼承自 Fragment,具體代碼如下:
public class MenuFragment extends Fragment implements OnItemClickListener {
/**
* 菜單界面中只包含了一個(gè)ListView。
*/
private ListView menuList;
/**
* ListView的適配器。
*/
private ArrayAdapter<String> adapter;
/**
* 用于填充ListView的數(shù)據(jù),這里就簡(jiǎn)單只用了兩條數(shù)據(jù)。
*/
private String[] menuItems = { "Sound", "Display" };
/**
* 是否是雙頁(yè)模式。如果一個(gè)Activity中包含了兩個(gè)Fragment,就是雙頁(yè)模式。
*/
private boolean isTwoPane;
/**
* 當(dāng)Activity和Fragment建立關(guān)聯(lián)時(shí),初始化適配器中的數(shù)據(jù)。
*/
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, menuItems);
}
/**
* 加載menu_fragment布局文件,為L(zhǎng)istView綁定了適配器,并設(shè)置了監(jiān)聽(tīng)事件。
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_fragment, container, false);
menuList = (ListView) view.findViewById(R.id.menu_list);
menuList.setAdapter(adapter);
menuList.setOnItemClickListener(this);
return view;
}
/**
* 當(dāng)Activity創(chuàng)建完畢后,嘗試獲取一下布局文件中是否有details_layout這個(gè)元素,如果有說(shuō)明當(dāng)前
* 是雙頁(yè)模式,如果沒(méi)有說(shuō)明當(dāng)前是單頁(yè)模式。
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (getActivity().findViewById(R.id.details_layout) != null) {
isTwoPane = true;
} else {
isTwoPane = false;
}
}
/**
* 處理ListView的點(diǎn)擊事件,會(huì)根據(jù)當(dāng)前是否是雙頁(yè)模式進(jìn)行判斷。如果是雙頁(yè)模式,則會(huì)動(dòng)態(tài)添加Fragment。
* 如果不是雙頁(yè)模式,則會(huì)打開(kāi)新的Activity。
*/
@Override
public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
if (isTwoPane) {
Fragment fragment = null;
if (index == 0) {
fragment = new SoundFragment();
} else if (index == 1) {
fragment = new DisplayFragment();
}
getFragmentManager().beginTransaction().replace(R.id.details_layout, fragment).commit();
} else {
Intent intent = null;
if (index == 0) {
intent = new Intent(getActivity(), SoundActivity.class);
} else if (index == 1) {
intent = new Intent(getActivity(), DisplayActivity.class);
}
startActivity(intent);
}
}
}
這個(gè)類的代碼并不長(zhǎng),我簡(jiǎn)單的說(shuō)明一下。在 onCreateView 方法中加載了 menu_fragment 這個(gè)布局,這個(gè)布局里面包含了一個(gè) ListView,然后我們對(duì)這個(gè) ListView 填充了兩個(gè)簡(jiǎn)單的數(shù)據(jù) "Sound" 和 "Display" 。又在 onActivityCreated 方法中做了一個(gè)判斷,如果 Activity 的布局中包含了 details_layout 這個(gè)元素,那么當(dāng)前就是雙頁(yè)模式,否則就是單頁(yè)模式。onItemClick 方法則處理了 ListView 的點(diǎn)擊事件,發(fā)現(xiàn)如果當(dāng)前是雙頁(yè)模式,就動(dòng)態(tài)往 details_layout 中添加 Fragment,如果當(dāng)前是單頁(yè)模式,就直接打開(kāi)新的 Activity。
我們把 MenuFragment 中引用到的其它內(nèi)容一個(gè)個(gè)添加進(jìn)來(lái)。新建 menu_fragment.xml 文件,加入如下代碼:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > android:id="@+id/menu_list" android:layout_width="fill_parent" android:layout_height="fill_parent"
然后新建 SoundFragment,里面內(nèi)容非常簡(jiǎn)單:
public class SoundFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sound_fragment, container, false);
這里 SoundFragment 需要用到 sound_fragment.xml 布局文件,因此這里我們新建這個(gè)布局文件,并加入如下代碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_centerInParent="true"
android:textSize="28sp"
android:textColor="#000000"
android:text="This is sound view"
/>
</RelativeLayout>
同樣的道理,我們?cè)傩陆?DisplayFragment 和 display_fragment.xml 布局文件:
public class DisplayFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.display_fragment, container, false);
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns: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="This is display view"
/>
</RelativeLayout>
然后新建 SoundActivity,代碼如下:
public class SoundActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sound_activity);
}
}
這個(gè) Activity 只是加載了一個(gè)布局文件,現(xiàn)在我們來(lái)實(shí)現(xiàn) sound_activity.xml 這個(gè)布局文件:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns: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>
這個(gè)布局文件引用了 SoundFragment,這樣寫(xiě)的好處就是,以后我們只需要在 SoundFragment 中修改代碼,SoundActivity 就會(huì)跟著自動(dòng)改變了,因?yàn)樗械拇a都是從 SoundFragment 中引用過(guò)來(lái)的。
好,同樣的方法,我們?cè)偻瓿?DisplayActivity:
public class DisplayActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_activity);
}
}
然后加入 display_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns: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>
四
現(xiàn)在所有的代碼就都已經(jīng)完成了,我們來(lái)看一下效果吧。
首先將程序運(yùn)行在手機(jī)上,效果圖如下:

分別點(diǎn)擊 Sound 和 Display,界面會(huì)跳轉(zhuǎn)到聲音和顯示界面:


然后將程序在平板上運(yùn)行,點(diǎn)擊 Sound,效果圖如下:

然后點(diǎn)擊 Display 切換到顯示界面,效果圖如下:

這樣我們就成功地讓程序同時(shí)兼容手機(jī)和平板了。當(dāng)然,這只是一個(gè)簡(jiǎn)單的 demo,更多復(fù)雜的內(nèi)容需要大家自己去實(shí)現(xiàn)了。
以上就是Android使用Fragment實(shí)現(xiàn)兼容手機(jī)和平板的程序的詳細(xì)內(nèi)容,更多關(guān)于Android 實(shí)現(xiàn)兼容手機(jī)和平板的程序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Android?Fragment實(shí)現(xiàn)頂部、底部導(dǎo)航欄
- Android Fragment源碼分析Add方法
- Android開(kāi)發(fā)之Fragment懶加載的幾種方式及性能對(duì)比
- Android入門(mén)教程之Fragment的具體使用詳解
- Android Fragment使用全解
- Android Fragment監(jiān)聽(tīng)返回鍵的一種合理方式
- 詳解Android studio 動(dòng)態(tài)fragment的用法
- Android Fragment實(shí)現(xiàn)底部通知欄
- Android Fragment的具體使用方式詳解
相關(guān)文章
Android NestedScrolling嵌套滾動(dòng)的示例代碼
這篇文章主要介紹了Android NestedScrolling嵌套滾動(dòng)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android實(shí)現(xiàn)一個(gè)簡(jiǎn)單的單詞本
大家好,本篇文章主要講的是Android實(shí)現(xiàn)一個(gè)簡(jiǎn)單的單詞本,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Android移動(dòng)端touch實(shí)現(xiàn)下拉刷新功能
這篇文章主要介紹了移動(dòng)端touch實(shí)現(xiàn)下拉刷新功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
Android 讀取文件內(nèi)容實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 讀取文件內(nèi)容實(shí)現(xiàn)方法的相關(guān)資料,這里提供了幾種方法,大家可以選擇使用,需要的朋友可以參考下2016-10-10
Android數(shù)據(jù)持久化之ContentProvider機(jī)制詳解
這篇文章主要介紹了Android數(shù)據(jù)持久化之ContentProvider機(jī)制,結(jié)合實(shí)例形式分析了ContentProvider機(jī)制的原理與相關(guān)使用技巧,需要的朋友可以參考下2017-05-05
anroid開(kāi)發(fā)教程之spinner下拉列表的使用示例
這篇文章主要介紹了anroid的spinner下拉列表的使用示例,需要的朋友可以參考下2014-04-04
Android sdutio配置Zxing進(jìn)行掃碼功能的實(shí)現(xiàn)方法
這篇文章主要介紹了Android sdutio配置Zxing進(jìn)行掃碼功能的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-05-05
Android使用socket創(chuàng)建簡(jiǎn)單TCP連接的方法
這篇文章主要介紹了Android使用socket創(chuàng)建簡(jiǎn)單TCP連接的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android使用socket創(chuàng)建TCP連接的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04
Android中使用Camera類編寫(xiě)手機(jī)拍照App的實(shí)例教程
這篇文章主要介紹了Android中使用Camera類編寫(xiě)手機(jī)拍照App的實(shí)例教程,整理了Camera調(diào)用硬件進(jìn)行拍照的一些常用方法,需要的朋友可以參考下2016-04-04

