Android中Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)創(chuàng)建步驟

本篇內(nèi)容主要講解“Android中Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)創(chuàng)建步驟”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Android中Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)創(chuàng)建步驟”吧!

目前成都創(chuàng)新互聯(lián)已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、雁峰網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

一、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="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=".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.java
package 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="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"  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.java
package 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="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=".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.java
package com.example.myapplication;import android.content.Context;import android.net.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="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=".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.java
package 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);ts.commit();

提交一個(gè)fragment事務(wù)。其核心是ts.add(……)方法。

詳細(xì)代碼如下:

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><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">  <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.java

package 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 (v.getId()){      case R.id.btn_dy1:        mBtnDy1.setBackgroundColor(0xff0000ff);        if(mfg1 == null){          mfg1 = new MyFragment1();          ts.add(R.id.fragment_container,mfg1);        }else {          ts.show(mfg1);        }        break;      case R.id.btn_dy2:        mBtnDy2.setBackgroundColor(0xff0000ff);        if(mfg2 == null){          mfg2 = new MyFragment2();          ts.add(R.id.fragment_container,mfg2);        }else {          ts.show(mfg2);        }        break;        default:          break;    }    ts.commit();  }//  將所有的Fragment都置為隱藏狀態(tài)。  private void hideFragments(FragmentTransaction transaction) {    if (mfg1 != null) {      transaction.hide(mfg1);    }    if (mfg2 != null) {      transaction.hide(mfg2);    }  }//   清除掉所有的選中狀態(tài)。  private void clearSelection() {    mBtnDy1.setBackgroundColor(0xffffffff);    mBtnDy2.setBackgroundColor(0xffffffff);  }}

到此,相信大家對(duì)“Android中Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)創(chuàng)建步驟”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

分享文章:Android中Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)創(chuàng)建步驟
網(wǎng)站路徑:http://www.muchs.cn/article30/jsoepo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、手機(jī)網(wǎng)站建設(shè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、域名注冊(cè)品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司