Fragment建立
1、靜態載入
1、fragment layout
2、fragment類
3、在對應的activity layout中載入 fragment
1、fragment layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radioGroup"
android:orientation="horizontal">
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@mipmap/ic_launcher"
android:text="靜態載入"
android:button="@null"
android:gravity="center_horizontal"
android:background="@drawable/radio_pressed"
android:id="@+id/btn_static"
/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@mipmap/ic_launcher"
android:text="動態載入"
android:button="@null"
android:gravity="center_horizontal"
android:background="@drawable/radio_pressed"
android:id="@+id/btn_dynamic"
/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@mipmap/ic_launcher"
android:text="生命週期"
android:button="@null"
android:gravity="center_horizontal"
android:background="@drawable/radio_pressed"
android:id="@+id/btn_life"
/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@mipmap/ic_launcher"
android:text="傳遞資料"
android:button="@null"
android:gravity="center_horizontal"
android:background="@drawable/radio_pressed"
android:id="@+id/btn_data"
/>
</RadioGroup>
</LinearLayout>
2、fragment class
package com.example.administrator.fragment2;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.zip.Inflater;
/**
* Created by Administrator on 2015/7/22.
*/
public class Fragment extends android.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
* 將 layout轉成 view物件
*/
View view = inflater.inflate(R.layout.fragment,container,false);
return view;
}
}
3.在對應的activity layout中載入 fragment
<RelativeLayout 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=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/fragment"
android:name="com.example.administrator.fragment2.Fragment"/>
</RelativeLayout>
2動態載入
前兩步和靜態載入一樣
不同的地方在於
package com.example.administrator.fragment2;
import android.app.*;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//動態載入 fragment
Fragment fragment = new Fragment(); //這個是我們自己定義的 Fragment
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null); //後退按鍵 允許
fragmentTransaction.add(R.id.linear,fragment);
fragmentTransaction.commit();
}
}
相關文章
- 動態建立FragmentFragment
- android Fragments詳解二:建立FragmentAndroidFragment
- Navigation問題詳解——Fragment建立新的例項NavigationFragment
- FragmentFragment
- Android_Fragment_Fragment詳解AndroidFragment
- Fragment demoFragment
- Fragment 知識梳理(1) Fragment 原始碼解析Fragment原始碼
- 談談Fragment的建構函式、重新建立(recreate)及相關Fragment函式
- 【BUG系列】Fragment中巢狀Fragment,不顯示Fragment巢狀
- Fragment初學5——使用Fragment的子類PreferenceFragmentFragment
- Fragment詳解Fragment
- Fragment隨筆Fragment
- fragment雜談Fragment
- Android--單Activity+多Fragment,玩轉FragmentAndroidFragment
- Fragment-踩坑Fragment
- Fragment生命週期Fragment
- fragment小認識Fragment
- Fragment的總結Fragment
- Android之FragmentAndroidFragment
- 安卓關閉fragment安卓Fragment
- Activity、Fragment和IntentFragmentIntent
- Fragment流程淺析Fragment
- 理解 Android FragmentAndroidFragment
- Fragment 原始碼解析Fragment原始碼
- oracle fragment conceptOracleFragment
- 使用fragment載入自定義fragment出現error inflating class fragment錯誤解決辦法FragmentError
- Fragment 知識梳理(2) Fragment 狀態儲存和恢復Fragment
- Android中Fragment巢狀Fragment,切換Fragment時不顯示檢視的原因及解決方法AndroidFragment巢狀
- fragment返回鍵關閉fragment以及最後一個fragment的時候關閉activity的簡便方法Fragment
- Android基礎—FragmentAndroidFragment
- 巧用Fragment解耦onActivityResultFragment解耦
- Fragment傳值到ActivityFragment
- 重寫返回鍵(Fragment)Fragment
- Fragment的生命週期Fragment
- fragment中listview的onitemclicklistenerFragmentView
- 監聽activity、fragment生命Fragment
- Android Fragment 深度解析AndroidFragment
- Fragment與Activity通訊Fragment