溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板

發(fā)布時間:2021-12-18 17:01:01 來源:億速云 閱讀:174 作者:iii 欄目:移動開發(fā)

本篇內(nèi)容介紹了“Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

我們先來看一下Android手機(jī)的設(shè)置界面,點(diǎn)擊一下Sound,可以跳轉(zhuǎn)到聲音設(shè)置界面,如下面兩張圖所示:

Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板

Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板

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

Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板

如果這分別是兩個不同的App做出的效果,那沒有絲毫驚奇之處。但如果是同一個App,在手機(jī)上和平板上運(yùn)行分別有以上兩種效果的話,你是不是就已經(jīng)心動了?我們現(xiàn)在就來模擬實(shí)現(xiàn)一下。

首先你需要對Fragment有一定的了解,如果你還沒接觸過Fragment,建議可以先閱讀 Android Fragment完全解析,關(guān)于碎片你所需知道的一切 這篇文章。并且本次的代碼是運(yùn)行在Android 4.0版本上的,如果你的SDK版本還比較低的話,建議可以先升升級了。

新建一個Android項(xiàng)目,取名叫FragmentDemo。打開或新建MainActivity作為程序的主Activity,里面有如下自動生成的內(nèi)容:

  1. publicclass MainActivity extends Activity {    

  2. @Override  

  3. publicvoid onCreate(Bundle savedInstanceState) {    

  4. super.onCreate(savedInstanceState);    

  5.         setContentView(R.layout.activity_main);    

  6.     }    

  7. }   

public class MainActivity extends Activity {     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);     } }

作為一個Android老手,上面的代碼實(shí)在太小兒科了,每個Activity中都會有這樣的代碼。不過今天我們的程序可不會這么簡單,加載布局這一塊還是大有文章的。

打開或新建res/layout/activity_main.xml作為程序的主布局文件,里面代碼如下:

  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 

  2. xmlns:tools="http://schemas.android.com/tools" 

  3. android:layout_width="fill_parent" 

  4. android:layout_height="fill_parent" 

  5. android:orientation="horizontal" 

  6. tools:context=".MainActivity"> 

  7. <fragment 

  8. android:id="@+id/menu_fragment" 

  9. android:name="com.example.fragmentdemo.MenuFragment" 

  10. android:layout_width="fill_parent" 

  11. android:layout_height="fill_parent" 

  12. /> 

  13. </LinearLayout>

<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>

這個布局引用了一個MenuFragment,我們稍后來進(jìn)行實(shí)現(xiàn),先來看一下今天的一個重點(diǎn),我們需要再新建一個activity_main.xml,這個布局文件名和前面的主布局文件名是一樣的,但是要放在不同的目錄下面。

別走開,下頁為您繼續(xù)介紹Fragment實(shí)現(xiàn)Android程序手機(jī)平板兼容

在res目錄下新建layout-large目錄,然后這個目錄下創(chuàng)建新的activity_main.xml,加入如下代碼:

  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 

  2. xmlns:tools="http://schemas.android.com/tools" 

  3. android:layout_width="fill_parent" 

  4. android:layout_height="fill_parent" 

  5. android:orientation="horizontal" 

  6. android:baselineAligned="false" 

  7. tools:context=".MainActivity" 

  8. <fragment 

  9. android:id="@+id/left_fragment" 

  10. android:name="com.example.fragmentdemo.MenuFragment" 

  11. android:layout_width="0dip" 

  12. android:layout_height="fill_parent" 

  13. android:layout_weight="1" 

  14. /> 

  15. <FrameLayout 

  16. android:id="@+id/details_layout" 

  17. android:layout_width="0dip" 

  18. android:layout_height="fill_parent" 

  19. android:layout_weight="3" 

  20. ></FrameLayout> 

  21. </LinearLayout>

<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>

這個布局同樣也引用了MenuFragment,另外還加入了一個FrameLayout用于顯示詳細(xì)內(nèi)容。其實(shí)也就是分別對應(yīng)了平板界面上的左側(cè)布局和右側(cè)布局。

這里用到了動態(tài)加載布局的技巧,首先Activity中調(diào)用 setContentView(R.layout.activity_main) ,表明當(dāng)前 的Activity想加載activity_main這個布局文件。而Android系統(tǒng)又會根據(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)于動態(tài)加載布局的更多內(nèi)容,可以閱讀 Android官方提供的支持不同屏幕大小的全部方法 這篇文章。

下面我們來實(shí)現(xiàn)久違的MenuFragment,新建一個MenuFragment類繼承自Fragment,具體代碼如下:

  1.     publicclass MenuFragment extends Fragment implements OnItemClickListener {   

  2.     /** 

  3.          * 菜單界面中只包含了一個ListView。 

  4.          */ 

  5.     private ListView menuList;   

  6.     /** 

  7.          * ListView的適配器。 

  8.          */ 

  9.     private ArrayAdapter<String> adapter;   

  10.     /** 

  11.          * 用于填充ListView的數(shù)據(jù),這里就簡單只用了兩條數(shù)據(jù)。 

  12.          */ 

  13.     private String[] menuItems = { "Sound", "Display" };   

  14.     /** 

  15.          * 是否是雙頁模式。如果一個Activity中包含了兩個Fragment,就是雙頁模式。 

  16.          */ 

  17.     privateboolean isTwoPane;   

  18.     /** 

  19.          * 當(dāng)Activity和Fragment建立關(guān)聯(lián)時,初始化適配器中的數(shù)據(jù)。 

  20.          */ 

  21.     @Override 

  22.     publicvoid onAttach(Activity activity) {   

  23.     super.onAttach(activity);   

  24.             adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, menuItems);   

  25.         }   

  26.     /** 

  27.          * 加載menu_fragment布局文件,為ListView綁定了適配器,并設(shè)置了監(jiān)聽事件。 

  28.          */ 

  29.     @Override 

  30.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {   

  31.             View view = inflater.inflate(R.layout.menu_fragment, container, false);   

  32.             menuList = (ListView) view.findViewById(R.id.menu_list);   

  33.             menuList.setAdapter(adapter);   

  34.             menuList.setOnItemClickListener(this);   

  35.     return view;   

  36.         }   

  37.     /** 

  38.          * 當(dāng)Activity創(chuàng)建完畢后,嘗試獲取一下布局文件中是否有details_layout這個元素,如果有說明當(dāng)前 

  39.          * 是雙頁模式,如果沒有說明當(dāng)前是單頁模式。 

  40.          */ 

  41.     @Override 

  42.     publicvoid onActivityCreated(Bundle savedInstanceState) {   

  43.     super.onActivityCreated(savedInstanceState);   

  44.     if (getActivity().findViewById(R.id.details_layout) != null) {   

  45.                 isTwoPane = true;   

  46.             } else {   

  47.                 isTwoPane = false;   

  48.             }   

  49.         }   

  50.     /** 

  51.          * 處理ListView的點(diǎn)擊事件,會根據(jù)當(dāng)前是否是雙頁模式進(jìn)行判斷。如果是雙頁模式,則會動態(tài)添加Fragment。 

  52.          * 如果不是雙頁模式,則會打開新的Activity。 

  53.          */ 

  54.     @Override 

  55.     publicvoid onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {   

  56.     if (isTwoPane) {   

  57.                 Fragment fragment = null;   

  58.     if (index == 0) {   

  59.                     fragment = new SoundFragment();   

  60.                 } elseif (index == 1) {   

  61.                     fragment = new DisplayFragment();   

  62.                 }   

  63. getFragmentManager().beginTransaction().replace(R.id.details_layout, fragment).commit();   

  64.             } else {   

  65.                 Intent intent = null;   

  66.     if (index == 0) {   

  67.                     intent = new Intent(getActivity(), SoundActivity.class);   

  68.                 } elseif (index == 1) {   

  69.                     intent = new Intent(getActivity(), DisplayActivity.class);   

  70.                 }   

  71.                 startActivity(intent);   

  72.             }   

  73.         }   

  74.     }  

public class MenuFragment extends Fragment implements OnItemClickListener {     /**      * 菜單界面中只包含了一個ListView。      */     private ListView menuList;     /**      * ListView的適配器。      */     private ArrayAdapter<String> adapter;     /**      * 用于填充ListView的數(shù)據(jù),這里就簡單只用了兩條數(shù)據(jù)。      */     private String[] menuItems = { "Sound", "Display" };     /**      * 是否是雙頁模式。如果一個Activity中包含了兩個Fragment,就是雙頁模式。      */     private boolean isTwoPane;      /**      * 當(dāng)Activity和Fragment建立關(guān)聯(lián)時,初始化適配器中的數(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布局文件,為ListView綁定了適配器,并設(shè)置了監(jiān)聽事件。      */     @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這個元素,如果有說明當(dāng)前      * 是雙頁模式,如果沒有說明當(dāng)前是單頁模式。      */     @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)擊事件,會根據(jù)當(dāng)前是否是雙頁模式進(jìn)行判斷。如果是雙頁模式,則會動態(tài)添加Fragment。      * 如果不是雙頁模式,則會打開新的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);         }     } }

這個類的代碼并不長,我簡單的說明一下。在onCreateView方法中加載了menu_fragment這個布局,這個布局里面包含 了一個ListView,然后我們對這個ListView填充了兩個簡單的數(shù)據(jù) "Sound" 和 "Display" 。又在 onActivityCreated方法中做了一個判斷,如果Activity的布局中包含了details_layout這個元素,那么當(dāng)前就是雙頁模 式,否則就是單頁模式。onItemClick方法則處理了ListView的點(diǎn)擊事件,發(fā)現(xiàn)如果當(dāng)前是雙頁模式,就動態(tài)往details_layout 中添加Fragment,如果當(dāng)前是單頁模式,就直接打開新的Activity。

別走開,下頁為您繼續(xù)介紹Fragment實(shí)現(xiàn)Android程序手機(jī)平板兼容

我們把MenuFragment中引用到的其它內(nèi)容一個個添加進(jìn)來。新建menu_fragment.xml文件,加入如下代碼:

  1. <?xmlversion="1.0"encoding="UTF-8"?> 

  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 

  3. android:layout_width="fill_parent" 

  4. android:layout_height="fill_parent"> 

  5. <ListView 

  6. android:id="@+id/menu_list" 

  7. android:layout_width="fill_parent" 

  8. android:layout_height="fill_parent" 

  9. ></ListView> 

  10. </LinearLayout>

<?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" >     <ListView         android:id="@+id/menu_list"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         ></ListView> </LinearLayout>

然后新建SoundFragment,里面內(nèi)容非常簡單:

  1. publicclass SoundFragment extends Fragment {   

  2. @Override 

  3. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {   

  4.         View view = inflater.inflate(R.layout.sound_fragment, container, false);   

  5. return view;   

  6.     }   

  7. }  

public class SoundFragment extends Fragment {     @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         View view = inflater.inflate(R.layout.sound_fragment, container, false);         return view;     } }

這里SoundFragment需要用到sound_fragment.xml布局文件,因此這里我們新建這個布局文件,并加入如下代碼:

  1. <?xmlversionxmlversion="1.0"encoding="utf-8"?>  

  2. ativeLayoutxmlns:androidRelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"  

  3. android:layout_width="match_parent"  

  4. android:layout_height="match_parent"  

  5. android:background="#00ff00"  

  6. android:orientation="vertical">  

  7. <TextView  

  8. android:layout_width="wrap_content"  

  9. android:layout_height="wrap_content"  

  10. android:layout_centerInParent="true"  

  11. android:textSize="28sp"  

  12. android:textColor="#000000"  

  13. android:text="This is sound view"  

  14. />  

  15. </RelativeLayout> 

<?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>

同樣的道理,我們再新建DisplayFragment和display_fragment.xml布局文件:

  1. publicclass DisplayFragment extends Fragment {   

  2. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {   

  3.         View view = inflater.inflate(R.layout.display_fragment, container, false);   

  4. return view;   

  5.     }   

  6. }  

  1. <?xmlversion="1.0"encoding="utf-8"?> 

  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" 

  3. android:layout_width="match_parent" 

  4. android:layout_height="match_parent" 

  5. android:background="#0000ff" 

  6. android:orientation="vertical"> 

  7. <TextView 

  8. android:layout_width="wrap_content" 

  9. android:layout_height="wrap_content" 

  10. android:layout_centerInParent="true" 

  11. android:textSize="28sp" 

  12. android:textColor="#000000" 

  13. android:text="This is display view" 

  14. /> 

  15. </RelativeLayout>

<?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,代碼如下:

  1. publicclass SoundActivity extends Activity {   

  2. @Override 

  3. protectedvoid onCreate(Bundle savedInstanceState) {   

  4. super.onCreate(savedInstanceState);   

  5.         setContentView(R.layout.sound_activity);   

  6.     }   

  7. }  

public class SoundActivity extends Activity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.sound_activity);     } }

這個Activity只是加載了一個布局文件,現(xiàn)在我們來實(shí)現(xiàn)sound_activity.xml這個布局文件:

  1. <?xmlversion="1.0"encoding="utf-8"?> 

  2. <fragmentxmlns:android="http://schemas.android.com/apk/res/android" 

  3. android:id="@+id/sound_fragment" 

  4. android:name="com.example.fragmentdemo.SoundFragment" 

  5. android:layout_width="match_parent" 

  6. android:layout_height="match_parent"> 

  7. </fragment>

<?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>

這個布局文件引用了SoundFragment,這樣寫的好處就是,以后我們只需要在SoundFragment中修改代碼,SoundActivity就會跟著自動改變了,因?yàn)樗械拇a都是從SoundFragment中引用過來的。

好,同樣的方法,我們再完成DisplayActivity:

  1. publicclass DisplayActivity extends Activity {   

  2. @Override 

  3. protectedvoid onCreate(Bundle savedInstanceState) {   

  4. super.onCreate(savedInstanceState);   

  5.         setContentView(R.layout.display_activity);   

  6.     }   

  7. }  

public class DisplayActivity extends Activity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.display_activity);     } }

別走開,下頁為您繼續(xù)介紹Fragment實(shí)現(xiàn)Android程序手機(jī)平板兼容

然后加入display_activity.xml:

  1. <?xmlversion="1.0"encoding="utf-8"?> 

  2. <fragmentxmlns:android="http://schemas.android.com/apk/res/android" 

  3. android:id="@+id/display_fragment" 

  4. android:name="com.example.fragmentdemo.DisplayFragment" 

  5. android:layout_width="match_parent" 

  6. android:layout_height="match_parent"> 

  7. </fragment>

<?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)完成了,我們來看一下效果吧。

首先將程序運(yùn)行在手機(jī)上,效果圖如下:

Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板

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

Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板

Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板

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

Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板

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

Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板

“Android程序怎么實(shí)現(xiàn)兼容手機(jī)和平板”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI