android Fragment入門

來源:互聯網
上載者:User

標籤: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入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.