亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android Fragment動(dòng)態(tài)創(chuàng)建詳解及示例代碼

 更新時(shí)間:2016年11月21日 17:15:05   作者:wuyudong  
這篇文章主要介紹了Android Fragment動(dòng)態(tài)創(chuàng)建詳解的相關(guān)資料,并附實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下

Android Fragment 動(dòng)態(tài)創(chuàng)建

Fragment是activity的界面中的一部分或一種行為??梢园讯鄠€(gè)Fragment組合到一個(gè)activity中來(lái)創(chuàng)建一個(gè)多界面并且可以在多個(gè)activity中重用一個(gè)Fragment??梢园袴ragment任務(wù)模塊化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity運(yùn)行時(shí)被添加或刪除。

Fragment不能獨(dú)立存在,它必須嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影響。例如:當(dāng)activity暫停時(shí),他擁有的所有的Fragment都暫停了,當(dāng)activity銷毀時(shí),他擁有的所有Fragment都被銷毀。然而,當(dāng)activity運(yùn)行時(shí)(在onResume()之后,onPause()之前),可以單獨(dú)地操作每個(gè)Fragment,比如添加或刪除它們。當(dāng)中執(zhí)行上述針對(duì)Fragment的事務(wù)時(shí),可以將事務(wù)添加到一個(gè)棧中,這個(gè)棧被activity管理,棧中的每一條都是一個(gè)Fragment的一次事務(wù)。有了這個(gè)棧,就可以反向執(zhí)行Fragment的事務(wù),這樣就可以在Fragment級(jí)支持“返回”鍵(向后導(dǎo)航)。

當(dāng)向activity中添加一個(gè)Fragment時(shí),它須置于ViewGroup控件中,并且需定義Fragment自己的界面??梢栽趌ayout.xml布局文件中聲明Fragment,元素為:<fragment>;也可以在代碼中創(chuàng)建Fragment,然后把它加入到ViewGroup控件中。然而,F(xiàn)ragment不一定非要放在activity的界面中,它可以隱藏在后臺(tái)為activity工作。

實(shí)戰(zhàn)一下

項(xiàng)目布局文件代碼:

<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:id="@+id/fragment1"
    android:name="com.wuyudong.fragment.Fragment1"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="1" >
  </fragment>

  <fragment
    android:id="@+id/fragment2"
    android:name="com.wuyudong.fragment.Fragment2"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="1" >
  </fragment>

</LinearLayout>

接著在layout文件夾下新建兩個(gè)fragment.xml文件

fragment1.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:background="#0000ff"
  android:orientation="vertical" >
</LinearLayout>

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:layout_height="match_parent"
  android:background="#ff00"
  android:orientation="vertical" >
</LinearLayout>

接著在項(xiàng)目源代碼文件夾下新建兩個(gè)java文件

Fragment1.java

public class Fragment1 extends Fragment {
  /**
   * 當(dāng)fragment被創(chuàng)建的時(shí)候,調(diào)用的方法,返回當(dāng)前fragment顯示的內(nèi)容 
   */
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment1, null);
  }

}

Fragment2.java

public class Fragment2 extends Fragment {
  /**
   * 當(dāng)fragment被創(chuàng)建的時(shí)候,調(diào)用的方法,返回當(dāng)前fragment顯示的內(nèi)容 
   */
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment2, null);
  }

}

運(yùn)行項(xiàng)目

接下來(lái)實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建Fragment

在剛才的項(xiàng)目的基礎(chǔ)上,新建一個(gè)項(xiàng)目。刪除原來(lái)activity_main.xml代碼中的fragment1與fragment2代碼段,其他的代碼不變

接下來(lái)在MainActivity中添加下面的代碼:

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 1、判斷當(dāng)前手機(jī)的朝向
    int width = getWindowManager().getDefaultDisplay().getWidth();
    int height = getWindowManager().getDefaultDisplay().getHeight();
    Fragment1 fragment1 = new Fragment1();
    Fragment2 fragment2 = new Fragment2();

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    if (width > height) {
      // 水平方向
      ft.replace(android.R.id.content, fragment1);
    } else {
      ft.replace(android.R.id.content, fragment2);
    }

    ft.commit();

  }

}

上面的代碼實(shí)現(xiàn)了當(dāng)手機(jī)不同朝向的時(shí)候,顯示的不同的fragment

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論