【Android開發】之Fragment開發1

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   使用   io   ar   

 

  一、Fragment簡介

    1.Fragment作為Activity介面的一部分組成出現;

    2.可以在一個Activity中同時出現多個Fragment,並且,一個Fragment亦可在多個Activity中使用;

    3.在Activity運行過程中,可以添加、移除或者替換Fragment(add()、remove()、replace());

    4.Fragment可以響應自己的輸入事件,並且有自己的生命週期,當然,它們的生命週期直接被其所屬的activity的生命週期影響。

    那我們為什麼要用Fragment呢?主要目的是用在大螢幕裝置上--例如平板電腦上,支援更加動態和靈活的UI設計。平板電腦的螢幕要比手機的大得多,有更多的空間來放更多的UI組件,並且這些組件之間會產生更多的互動。我們可以把Fragment認為是“小的Activity”,Fragment更加簡潔。

 

  二、Fragment的簡單使用

    那我們就簡單的顯示2個Fragment為例來講解一下。

    

    1.在XML中添加Fragment:

      建立Fragment1、Fragment2(注意這裡匯入的是android.app.Fragment的Fragment):

      Fragment1代碼:

 1 package com.example.fragment; 2  3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.util.Log; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 10 import com.example.fragmentdemo.R;11 12 public class Fragment1 extends Fragment {13     @Override14     public View onCreateView(LayoutInflater inflater, ViewGroup container,15             Bundle savedInstanceState) {16         Log.e("TAG", "in");17         return inflater.inflate(R.layout.fragment1, container, false);18     }19 }
View Code

      Fragment2代碼:

 1 package com.example.fragment; 2  3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8  9 import com.example.fragmentdemo.R;10 11 public class Fragment2 extends Fragment {12     @Override13     public View onCreateView(LayoutInflater inflater, ViewGroup container,14             Bundle savedInstanceState) {15         return inflater.inflate(R.layout.fragment2, container, false);16     }17 }
View Code

      Fragment1的xml代碼:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:background="#FF69B4" 6     android:orientation="vertical" > 7  8     <TextView 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         android:layout_gravity="center"12         android:text="這是第一個Fragment" />13 14 </LinearLayout>
View Code

      Fragment2的xml代碼:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:background="#EECBAD" 6     android:orientation="vertical" > 7  8     <TextView 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         android:layout_gravity="center"12         android:text="這是第二個Fragment" />13 14 </LinearLayout>
View Code

      我們在activity_main.xml中添加兩個Fragment,代碼如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     android:layout_width="match_parent" 3     android:layout_height="match_parent" 4     android:baselineAligned="false" > 5  6     <fragment 7         android:id="@+id/fragment1" 8         android:name="com.example.fragment.Fragment1" 9         android:layout_width="wrap_content"10         android:layout_height="match_parent"11         android:layout_weight="1" />12 13     <fragment14         android:id="@+id/fragment2"15         android:name="com.example.fragment.Fragment2"16         android:layout_width="wrap_content"17         android:layout_height="match_parent"18         android:layout_weight="1" />19 20 </LinearLayout>
View Code

      MainActivity代碼如下:

 1 package com.example.fragmentdemo; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5  6 public class MainActivity extends Activity { 7  8     @Override 9     protected void onCreate(Bundle savedInstanceState) {10         super.onCreate(savedInstanceState);11         setContentView(R.layout.activity_main);12     }13 }
View Code

      然後運行工程就可以顯示Fragment了,下面是。

 

    2.動態添加Fragment:

      

      

 

【Android開發】之Fragment開發1

聯繫我們

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