




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
【移動應用開發(fā)技術(shù)】AndroidFragment中怎么創(chuàng)建靜態(tài)注冊和動態(tài)注冊
這篇文章主要介紹了AndroidFragment中怎么創(chuàng)建靜態(tài)注冊和動態(tài)注冊,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓在下帶著大家一起了解一下。一、fragment靜態(tài)注冊創(chuàng)建方法及步驟1.創(chuàng)建一個StaticFragment.java文件繼承Fragment類和一個static_fragment.xml文件完成fragment的布局。在StaticFragment.java中重載onCreateView(……)方法,通過調(diào)用inflater.inflate(……)方法并傳入布局資源ID生成fragment的視圖資源,并綁定static_fragment.xml中的相關(guān)組件然后實現(xiàn)其功能。實現(xiàn)代碼如下:static_fragment.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StaticFragment"
android:orientation="vertical">
<Button
android:id="@+id/btn_fm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="這是fragment靜態(tài)注冊"
android:textAllCaps="false">
</Button>
<EditText
android:id="@+id/et_fm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入你要改變的內(nèi)容:">
</EditText>
</LinearLayout>StaticFragment.javapackage
com.example.myapplication;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.Button;
import
android.widget.EditText;
import
androidx.annotation.NonNull;
import
androidx.annotation.Nullable;
import
androidx.fragment.app.Fragment;
public
class
StaticFragment
extends
Fragment
{
private
Button
mBtnFm;
private
EditText
mEtFm;
@Nullable
@Override
public
View
onCreateView(@NonNull
LayoutInflater
inflater,
@Nullable
ViewGroup
container,
@Nullable
Bundle
savedInstanceState)
{
//fragment的視圖資源是直接通過調(diào)用inflater.inflate(……)方法并傳入布局資源ID生成的。
View
v
=
inflater.inflate(R.layout.static_fragment,
container,false);
mEtFm
=
v.findViewById(R.id.et_fm);
mBtnFm
=
v.findViewById(R.id.btn_fm);
mBtnFm.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
v)
{
mBtnFm.setText(mEtFm.getText().toString());
}
});
return
v;
}
}2.在主布局activity_main.xml文件中綁定fragment布局文件。實現(xiàn)代碼如下:activity_main.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="這是主布局"
android:textColor="@color/colorAccent"
android:textSize="30sp">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下面是fragment的布局"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp">
</TextView>
<fragment
android:id="@+id/static_fm"
android:name="com.example.myapplication.StaticFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</fragment>
</LinearLayout>注意:布局文件中加fragment節(jié)點,name屬性必須填寫完整的路徑MainActivity.javapackage
com.example.myapplication;
import
androidx.appcompat.app.AppCompatActivity;
import
android.os.Bundle;
public
class
MainActivity
extends
AppCompatActivity
{
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}演示:二、fragment動態(tài)注冊創(chuàng)建方法及步驟1.新建一個項目,創(chuàng)建2個Fragment繼承類分別為MyFragment1.java和MyFragment2.java,然后創(chuàng)建2個布局文件分別為fragment1.xml和fragment2.xml.詳細代碼如下:fragment1.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyFragment1"
android:gravity="center"
android:background="@color/colorPrimaryDark">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/hello_blank_fragment"
android:textSize="30sp"
android:textAllCaps="false"
android:textColor="#F70505">
</TextView>
</LinearLayout>MyFragment1.javapackage
com.example.myapplication;
import
android.content.Context;
import
.Uri;
import
android.os.Bundle;
import
androidx.fragment.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
public
class
MyFragment1
extends
Fragment
{
@Override
public
View
onCreateView(LayoutInflater
inflater,
ViewGroup
container,
Bundle
savedInstanceState)
{
//
Inflate
the
layout
for
this
fragment
return
inflater.inflate(R.layout.fragment1,
container,
false);
}
}fragment2.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyFragment2"
android:gravity="center"
android:background="@color/colorAccent">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/hello_blank_fragment"
android:textSize="30sp"
android:textAllCaps="false"
android:textColor="#03FAE3">
</TextView>
</LinearLayout>MyFragment2.javapackage
com.example.myapplication;
import
android.os.Bundle;
import
androidx.fragment.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
public
class
MyFragment2
extends
Fragment
{
@Override
public
View
onCreateView(LayoutInflater
inflater,
ViewGroup
container,
Bundle
savedInstanceState)
{
//
Inflate
the
layout
for
this
fragment
return
inflater.inflate(R.layout.fragment2,
container,
false);
}
}上述代碼與靜態(tài)創(chuàng)建的區(qū)別不大。2.以代碼的形式將fragment添加到activity需要在activity里直接調(diào)用FragmentManager。FragmentManager
fm
=
getSupportFragmentManager();然后通過代碼塊:FragmentTransaction
ts
=
fm.beginTransaction();
Fragment
mfg1
=
new
MyFragment1();
ts.add(R.id.fragment_container,mfg1);
mit();提交一個fragment事務。其核心是ts.add(……)方法。詳細代碼如下:activity_main.xml:<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/btn_dy1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1"
android:textColor="@color/colorAccent"
android:textSize="30sp">
</Button>
<Button
android:id="@+id/btn_dy2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp">
</Button>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/linear">
</FrameLayout>
</RelativeLayout>注意:fragment模塊一般用FrameLayout布局承載MainActivity.javapackage
com.example.myapplication;
import
androidx.appcompat.app.AppCompatActivity;
import
androidx.fragment.app.Fragment;
import
androidx.fragment.app.FragmentManager;
import
androidx.fragment.app.FragmentTransaction;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
public
class
MainActivity
extends
AppCompatActivity
implements
View.OnClickListener
{
private
Button
mBtnDy1;
private
Button
mBtnDy2;
FragmentManager
fm;
Fragment
mfg1;
Fragment
mfg2;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fm
=
getSupportFragmentManager();
mBtnDy1
=
findViewById(R.id.btn_dy1);
mBtnDy2
=
findViewById(R.id.btn_dy2);
mBtnDy1.setOnClickListener(this);
mBtnDy2.setOnClickListener(this);
}
@Override
public
void
onClick(View
v)
{
clearSelection();//清除按鈕狀態(tài)
FragmentTransaction
ts
=
fm.beginTransaction();
hideFragments(ts);
switch
(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 學習心理與教學方法的優(yōu)化研究
- 混合學習未來教育的創(chuàng)新模式
- 全球化背景下國際教育的政策支持研究
- 中國教育培訓機構(gòu)市場競爭策略分析
- 心理驅(qū)動教育心理學在學生自我管理中的應用
- 教育技術(shù)趨勢報告提升教學效果的科技工具
- 基于創(chuàng)新教育政策的科技產(chǎn)業(yè)未來規(guī)劃及戰(zhàn)略部署探討
- 智慧辦公未來校園管理的新趨勢
- 商業(yè)培訓中的教育創(chuàng)新提升培訓效果的方法
- 教育科技與心理輔導的結(jié)合策略探索
- 云南省曲靖市宣威市民中2025屆高一化學第二學期期末檢測試題含解析
- 2024年寧夏銀川金鳳區(qū)社區(qū)專職工作者考試真題
- 2025至2030全球及中國帆船行業(yè)產(chǎn)業(yè)運行態(tài)勢及投資規(guī)劃深度研究報告
- 北京海淀街道社區(qū)衛(wèi)生服務中心招聘筆試真題2024
- 新疆天富能源股份有限公司2024年度商譽減值測試資產(chǎn)評估報告
- 泄密警示教育專題培訓
- 腫瘤標志物實驗室解讀
- 2025年中國水下測深儀市場調(diào)查研究報告
- 2025年湖北省中考英語試卷真題(含答案)
- 2023衡水市事業(yè)單位考試歷年真題
評論
0/150
提交評論