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();
}
}
相關文章
- Navigation問題詳解——Fragment建立新的例項NavigationFragment
- FragmentFragment
- 【BUG系列】Fragment中巢狀Fragment,不顯示Fragment巢狀
- Android--單Activity+多Fragment,玩轉FragmentAndroidFragment
- Android之FragmentAndroidFragment
- 使用fragment載入自定義fragment出現error inflating class fragment錯誤解決辦法FragmentError
- Android中Fragment巢狀Fragment,切換Fragment時不顯示檢視的原因及解決方法AndroidFragment巢狀
- Activity、Fragment和IntentFragmentIntent
- Android基礎—FragmentAndroidFragment
- Fragment的總結Fragment
- Fragment-踩坑Fragment
- Fragment生命週期Fragment
- fragment小認識Fragment
- fragment返回鍵關閉fragment以及最後一個fragment的時候關閉activity的簡便方法Fragment
- Fragment翻蓋動效Fragment
- Android 碎片(Fragment)講解AndroidFragment
- 安卓關閉fragment安卓Fragment
- Fragment傳值到ActivityFragment
- 重寫返回鍵(Fragment)Fragment
- 巧用Fragment解耦onActivityResultFragment解耦
- 自定義Navigator切換fragmentFragment
- Android 點將臺:撒豆成兵[- Fragment -]AndroidFragment
- Fragment跳轉的騷操作Fragment
- Android Jetpack - Fragment官方說明AndroidJetpackFragment
- Fragment 懶載入實踐Fragment
- 【Android Fragment】友盟統計 Fragment 頁面顯示隱藏的完美解決方案AndroidFragment
- Android ViewPager2 + Fragment 聯動AndroidViewpagerFragment
- Fragment時長統計那些事Fragment
- Android面試題之Fragment篇Android面試題Fragment
- 理解 Fragment 的應用場景Fragment
- Fragment中呼叫startActivityForResult的那些坑Fragment
- ViewPager巢狀fragment簡單使用Viewpager巢狀Fragment
- 1-AVI–Fragment基礎使用Fragment
- Android優化--Fragment懶載入Android優化Fragment
- 【Android Fragment】解決ViewPager巢狀時Fragment的mUserVisibleHint屬性不同步的問題AndroidFragmentViewpager巢狀
- Android使用(TabLayout+ViewPager+fragment)與(FragmentTabHost+ViewPager+Fragment)實現底部狀態列切換AndroidTabLayoutViewpagerFragment
- 探索 單個Fragment實現沉浸式,其餘Fragment不實現的解決辦法Fragment
- 【Android Fragment】解決Fragment多層巢狀時onActivityResult無法正確回撥的問題AndroidFragment巢狀
- Activity和Fragment有什麼區別Fragment