




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
【移動應(yī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事務(wù)。其核心是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)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024-2025學年山東省泰安市高新區(qū)良莊二中學數(shù)學七上期末達標檢測模擬試題含解析
- 天津城建大學《現(xiàn)代分子生物學技術(shù)》2023-2024學年第一學期期末試卷
- 四川文軒職業(yè)學院《商務(wù)談判與溝通》2023-2024學年第一學期期末試卷
- 山東省德州市經(jīng)濟開發(fā)區(qū)抬頭寺中學2024年七年級數(shù)學第一學期期末考試試題含解析
- 河南省信陽市長竹園一中學2024年數(shù)學七年級第一學期期末綜合測試試題含解析
- 河南師范大學《節(jié)目主持藝術(shù)》2023-2024學年第一學期期末試卷
- 貴州航天職業(yè)技術(shù)學院《殘障人權(quán)益保護》2023-2024學年第一學期期末試卷
- 浙江特殊教育職業(yè)學院《數(shù)據(jù)分析方法》2023-2024學年第一學期期末試卷
- 調(diào)色師考試試題及答案
- 電廠電工考試試題及答案
- 《國有企業(yè)招投標及采購管理辦法》
- GB/T 16451-2008天然脂肪醇
- GB 5013.2-1997額定電壓450/750V及以下橡皮絕緣電纜第2部分:試驗方法
- 普通高中物理課程標準
- 國家開放大學《監(jiān)督學》形考任務(wù)( 1-4)試題和答案解析
- 完工付款最終付款申請表
- 人工動靜脈內(nèi)瘺
- 新版(七步法案例)PFMEA
- 慢阻肺隨訪記錄表正式版
- 廣西大學數(shù)學建模競賽選拔賽題目
- 受戒申請表(共3頁)
評論
0/150
提交評論