詳解安卓Fragment(片段化),

來源:互聯網
上載者:User

詳解安卓Fragment(片段化),

  Fragment從字面意思理解就是片段的意思,當然是為瞭解決安卓各類裝置片段化嚴重的問題,比如同樣一個App在手機上顯示效果還不錯,但是一旦上了16:9的平板立刻就變了味,使用安卓平板的同學可能體(bei)會(keng)更深,為此Google官方從android 3.0(對應API 11)引入Fragment,簡單理解就是把介面分割成很多片段,然後根據實際要求最後選擇性的進行拼接,比如在手機豎屏模式下只能顯示一本書的目錄列表,但是如果是橫屏模式(也可以立即為平板電腦模式)下就可以在螢幕的左半邊顯示目錄列表,然後在螢幕的右半邊顯示每個清單項目的簡介,這樣使用者體驗就會好很多。好了。下面開始實現一個簡單的Fragment Demo

最後顯示效果如下:(一個平板電腦模式下左半邊顯示fragment1,右邊顯示fragment2)


1:分別建立fragment1和fragment2的布局

fragment1.xml

<span style="font-size:18px;"><?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:text="這是fragment1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20sp"        android:textColor="#00CD00"/>    </LinearLayout></span>

fragment2.xml
<span style="font-size:18px;"><?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:text="這是fragment2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20sp"        android:textColor="#EEEE00"/>    </LinearLayout></span>



2:這裡僅僅是聲明了靜態介面,將來要引用的話還需要建立繼承自Fragment的Activity(fragment1和fragment2都要建立)

Fragment1.class

<span style="font-size:18px;">package com.example.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment{   @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubreturn inflater.inflate(R.layout.fragment, container,false) ;}}</span>


<span style="font-size:18px;"></span>

Fragment2.class

<span style="font-size:18px;">package com.example.fragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubreturn inflater.inflate(R.layout.fragment2, container, false) ;}}</span>



3:到此基礎的兩個片段都已經實現完畢,現在開始建立主介面局部

activity_main.xml

<span style="font-size:18px;"><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="horizontal"    android:baselineAligned="false"    tools:context="com.example.fragment.MainActivity" >   <fragment        android:id="@+id/fragment1"       android:name="com.example.fragment.Fragment1"       android:layout_height="wrap_content"       android:layout_width="0dp"       android:layout_weight="1"/>       <fragment        android:id="@+id/fragment2"       android:name="com.example.fragment.Fragment2"       android:layout_height="wrap_content"       android:layout_width="0dp"       android:layout_weight="1"/></LinearLayout></span>

最後簡單寫一下主Activity啟動項目
<span style="font-size:18px;">package com.example.fragment;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends ActionBarActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}</span>


項目運行就不貼了。





安卓各個版本API level與版本的對應關係。

Platform Version API Level VERSION_CODE Notes
Android 4.4 19 KITKAT Platform Highlights
Android 4.3 18 JELLY_BEAN_MR2 Platform Highlights
Android 4.2, 4.2.2 17 JELLY_BEAN_MR1 Platform Highlights
Android 4.1, 4.1.1 16 JELLY_BEAN Platform Highlights
Android 4.0.3, 4.0.4 15 ICE_CREAM_SANDWICH_MR1 Platform Highlights
Android 4.0, 4.0.1, 4.0.2 14 ICE_CREAM_SANDWICH
Android 3.2 13 HONEYCOMB_MR2
Android 3.1.x 12 HONEYCOMB_MR1 Platform Highlights
Android 3.0.x 11 HONEYCOMB Platform Highlights
Android 2.3.4
Android 2.3.3
10 GINGERBREAD_MR1 Platform Highlights
Android 2.3.2
Android 2.3.1
Android 2.3
9 GINGERBREAD
Android 2.2.x 8 FROYO Platform Highlights
Android 2.1.x 7 ECLAIR_MR1 Platform Highlights
Android 2.0.1 6 ECLAIR_0_1
Android 2.0 5 ECLAIR
Android 1.6 4 DONUT Platform Highlights
Android 1.5 3 CUPCAKE Platform Highlights
Android 1.1 2 BASE_1_1
Android 1.0 1 BASE



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.