Android百議程序:嵌入Fragment

來源:互聯網
上載者:User

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

Fragment相當於一個小型activity,因為Fragment可以實現activity中所有的功能,不同的是Fragment可以嵌入activity,一個activity可以有多個Fragment,而且可以運行時根據需要切換Fragment,達到可以適應不同螢幕大小等目的需要。


本章來看看如何在activity中嵌入Fragment的方法,有靜態和動態方法的。

靜態方法就是使用xml直接嵌入,動態就是在Java代碼中嵌入。


方法:

1 按照預設設定建立項目

2 建立一個類命名為Fragment1.java

代碼如下:

package bill.su.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) {return inflater.inflate(R.layout.fragment1, container, false);}}
3 建立layout的xml檔案,輸入代碼如下:

<?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"    android:background="#00FF00" >        <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/fragment_one"        android:textSize="25sp"        android:textColor="#FF0000" /></LinearLayout>

4 同理建立第二個類:

package bill.su.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) {return inflater.inflate(R.layout.fragment2, container, false);}}

5 建立第二個類的對應layout的xml檔案:

<?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"    android:background="#FFFE00" >        <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/fragment_two"        android:textColor="#000000"        android:textSize="25sp" />    </LinearLayout>

6 只需要在主activity的xml檔案中直接嵌入Fragment就可以了:

<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"    tools:context="bill.su.fragment.MainActivity"    android:orientation="horizontal" >    <fragment        android:name="bill.su.fragment.Fragment1"        android:id="@+id/fragment1"        android:layout_height="match_parent"        android:layout_weight="1"        android:layout_width="0dp" />       <fragment        android:name="bill.su.fragment.Fragment2"        android:id="@+id/fragment2"        android:layout_weight="1"        android:layout_width="0sp"        android:layout_height="match_parent" /></LinearLayout>

運行得到如下效果:



這樣同一個activity中嵌入了兩個不同的Fragment了。

這樣基本上不用寫Java代碼。


如果在activity的Java代碼中實現的話,就只需要修改onCreate函數就可以:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);FragmentManager fragManager = getFragmentManager();FragmentTransaction fragTrans = fragManager.beginTransaction();// get the current display infoWindowManager wm = getWindowManager();Display d = wm.getDefaultDisplay();if (d.getWidth() < d.getHeight()) {Fragment1 frag1 = new Fragment1();fragTrans.replace(android.R.id.content, frag1);}else {Fragment2 frag2 = new Fragment2();fragTrans.replace(android.R.id.content, frag2);}fragTrans.commit();}

知識點:

使用FragmentManager和FragmentTransaction兩個類實現動態載入Fragment。

其中的WindowManager是為了得到當前螢幕的長和寬,判斷是橫屏韓式豎屏,並根據這判斷載入不同的Fragment,得到不同的效果。

最後一句FragTrans.commit()是必須的,這樣才能使得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.