標籤:android style blog http io color ar os 使用
Fragment是android3.0引入的,為什麼google推出Fragment呢?主要目的是用在大螢幕裝置上--例如平板電腦上,支援更加動態和靈活的UI設計。平板電腦的螢幕要比手機的大得多,有更多的空間來放更多的UI組件,並且這些組件之間會產生更多的互動,Fragment允許這樣的一種設計,而不需要你親自來管理 viewhierarchy的複雜變化。 通過將activity的布局分散到fragment中, 你可以在運行時修改activity的外觀,可以把Fragment看作是activity介面上的一部分,首先看:
第一張圖我們看到,點擊左邊的item跳轉到右邊的布局上顯示,這時候就要啟動一個activity,而下面的圖點擊左邊的item,可以在右邊顯示,用Fragment來顯示就行,而不用啟動activity,我們知道activity是android的組件,所以它比Fragment佔用的記憶體就大,這就是為什麼在大點的螢幕推薦使用Fragment
現在建立一個Android項目Fragment1
activity_main.xml
<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" tools:context=".MainActivity" > <fragment android:name="com.example.fragment1.Fragment1" android:id="@+id/fragment1" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:name="com.example.fragment1.Fragment2" android:id="@+id/fragment2" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /></LinearLayout>
在布局中發現一個節點fragment,而我們以前layout中view都是大寫字母開頭,比如:TextView,所以fragment並不是一個view對象,而是一種類型,android:name指的是Fragment類的全類名,所以Fragment1要繼承Fragment對象,如果是使用 android.app.Fragment包下的,那麼指定的最小版本必須是11(android:minSdkVersion="11")小於11程式就會報錯,因為系統的Fragment是3.0出現的,
Fragment1.java
package com.example.fragment1;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) {return inflater.inflate(R.layout.fragment1, null);}}
fragment1.xml
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#ff0000" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是第一個fragment" android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>
Fragment2.java
package com.example.fragment1;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) {return inflater.inflate(R.layout.fragment2, null);}}
fragment2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#0000ff" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是第二個fragment" android:textAppearance="?android:attr/textAppearanceLarge" /></LinearLayout>
MainActivity.java
package com.example.fragment1;import android.os.Bundle;import android.app.Activity;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
我們看到MainActivity類中並沒有寫任何代碼,這是靜態建立Fragment,:
android Fragment入門