国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android Fragment動態創建詳解及示例代碼

2019-12-12 04:33:44
字體:
來源:轉載
供稿:網友

Android Fragment 動態創建

Fragment是activity的界面中的一部分或一種行為。可以把多個Fragment組合到一個activity中來創建一個多界面并且可以在多個activity中重用一個Fragment。可以把Fragment任務模塊化的一段activity,它具有自己的生命周期,接收它自己的事件,并可以在activity運行時被添加或刪除。

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

當向activity中添加一個Fragment時,它須置于ViewGroup控件中,并且需定義Fragment自己的界面。可以在layout.xml布局文件中聲明Fragment,元素為:<fragment>;也可以在代碼中創建Fragment,然后把它加入到ViewGroup控件中。然而,Fragment不一定非要放在activity的界面中,它可以隱藏在后臺為activity工作。

實戰一下

項目布局文件代碼:

<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文件夾下新建兩個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>

接著在項目源代碼文件夾下新建兩個java文件

Fragment1.java

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

Fragment2.java

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

運行項目

接下來實現動態創建Fragment

在剛才的項目的基礎上,新建一個項目。刪除原來activity_main.xml代碼中的fragment1與fragment2代碼段,其他的代碼不變

接下來在MainActivity中添加下面的代碼:

public class MainActivity extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    // 1、判斷當前手機的朝向    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();  }}

上面的代碼實現了當手機不同朝向的時候,顯示的不同的fragment

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌什县| 慈利县| 济阳县| 和田市| 沂水县| 泰宁县| 雷州市| 辛集市| 凯里市| 辽源市| 乌兰浩特市| 饶河县| 灵川县| 平阴县| 即墨市| 遂平县| 吉安县| 潞城市| 洱源县| 平湖市| 嫩江县| 大关县| 如皋市| 濮阳市| 工布江达县| 汉川市| 文安县| 临安市| 宝应县| 寻乌县| 宜阳县| 芜湖市| 葫芦岛市| 镇赉县| 左贡县| 平潭县| 道孚县| 固原市| 潜山县| 扬州市| 沁源县|