




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】AndroidFragment中怎么創(chuàng)建靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)
這篇文章主要介紹了AndroidFragment中怎么創(chuàng)建靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè),具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓在下帶著大家一起了解一下。一、fragment靜態(tài)注冊(cè)創(chuàng)建方法及步驟1.創(chuàng)建一個(gè)StaticFragment.java文件繼承Fragment類和一個(gè)static_fragment.xml文件完成fragment的布局。在StaticFragment.java中重載onCreateView(……)方法,通過調(diào)用inflater.inflate(……)方法并傳入布局資源ID生成fragment的視圖資源,并綁定static_fragment.xml中的相關(guān)組件然后實(shí)現(xiàn)其功能。實(shí)現(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)注冊(cè)"
android:textAllCaps="false">
</Button>
<EditText
android:id="@+id/et_fm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入你要改變的內(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布局文件。實(shí)現(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é)點(diǎn),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動(dòng)態(tài)注冊(cè)創(chuàng)建方法及步驟1.新建一個(gè)項(xiàng)目,創(chuàng)建2個(gè)Fragment繼承類分別為MyFragment1.java和MyFragment2.java,然后創(chuàng)建2個(gè)布局文件分別為fragment1.xml和fragment2.xml.詳細(xì)代碼如下: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();提交一個(gè)fragment事務(wù)。其核心是ts.add(……)方法。詳細(xì)代碼如下: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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 果蔬銷售中的智能物流與倉儲(chǔ)管理考核試卷
- 江蘇省南京市玄武外國語校2024-2025學(xué)年初三5月第二次月考生物試題含解析
- 吉林省白城市大安市第二中學(xué)2025屆高三第二學(xué)期期末(一模)英語試題含解析
- 四川工程職業(yè)技術(shù)學(xué)院《無線傳感器網(wǎng)絡(luò)》2023-2024學(xué)年第二學(xué)期期末試卷
- 宿遷學(xué)院《外科學(xué)實(shí)驗(yàn)》2023-2024學(xué)年第一學(xué)期期末試卷
- 江蘇商貿(mào)職業(yè)學(xué)院《信息理論與編碼》2023-2024學(xué)年第二學(xué)期期末試卷
- 新疆維吾爾自治區(qū)喀什二中2024-2025學(xué)年招生全國統(tǒng)一考試高考仿真模擬卷數(shù)學(xué)試題(全國)試題含解析
- 吉林鐵道職業(yè)技術(shù)學(xué)院《傳統(tǒng)視覺藝術(shù)與現(xiàn)代設(shè)計(jì)》2023-2024學(xué)年第二學(xué)期期末試卷
- 新余市渝水區(qū)2025屆三年級(jí)數(shù)學(xué)第二學(xué)期期末學(xué)業(yè)質(zhì)量監(jiān)測(cè)試題含解析
- 江西科技師范大學(xué)《植物生物技術(shù)綜合實(shí)驗(yàn)》2023-2024學(xué)年第二學(xué)期期末試卷
- ERP項(xiàng)目可行性研究報(bào)告(可編輯)
- 10《奪取抗日戰(zhàn)爭(zhēng)和人民解放戰(zhàn)爭(zhēng)的勝利》說課稿-2023-2024學(xué)年道德與法治五年級(jí)下冊(cè)
- 上海市工業(yè)技術(shù)學(xué)校工作人員招考聘用高頻重點(diǎn)提升(共500題)附帶答案詳解
- (完整版)信號(hào)與系統(tǒng)(吳大正)-完整版答案-糾錯(cuò)修改后版本
- 2024年第四季度 國家電網(wǎng)工程設(shè)備材料信息參考價(jià)
- 【八年級(jí)下冊(cè)地理中圖北京版】期中真題必刷卷A-【期中真題必刷卷】(北京專用)(解析版)
- 足球俱樂部青訓(xùn)管理制度
- 《質(zhì)量成本培訓(xùn)教材》課件
- 人教版-八年級(jí)數(shù)學(xué)上冊(cè)-競(jìng)賽專題分式方程(含答案)
- 無文件木馬技術(shù)分析-洞察分析
- 陜西省西安市西北大學(xué)附中2025屆高考仿真卷英語試題含解析
評(píng)論
0/150
提交評(píng)論