欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年02月06日 10:26:00   作者:大鵬學(xué)Android  
這篇文章主要介紹了Android Fragment的靜態(tài)注冊和動態(tài)注冊創(chuàng)建步驟,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

一、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="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)注冊"
    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.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布局文件。

實現(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é)點,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動態(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="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();

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

詳細代碼如下:

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);
  }
}

演示:

總結(jié)

以上所述是小編給大家介紹的Android Fragment的靜態(tài)注冊和動態(tài)注冊創(chuàng)建步驟,希望對大家有所幫助!

相關(guān)文章

  • Android圓形旋轉(zhuǎn)菜單開發(fā)實例

    Android圓形旋轉(zhuǎn)菜單開發(fā)實例

    本文給大家分享一個動畫菜單,基于android開發(fā)圓形旋轉(zhuǎn)菜單案例,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧
    2016-09-09
  • android照相、相冊獲取圖片剪裁報錯的解決方法

    android照相、相冊獲取圖片剪裁報錯的解決方法

    最近在項目中用到了照相和相冊取圖剪裁上傳頭像,就在網(wǎng)上逛了逛,基本都是千篇一律,就弄下來用了用,沒想到的是各種各樣的奇葩問題就出現(xiàn)了。先給大家看看代碼問題慢慢來解決
    2014-11-11
  • android textview 顯示html方法解析

    android textview 顯示html方法解析

    現(xiàn)在網(wǎng)絡(luò)的繁盛時代,光文字是不能滿足人們的胃口的,圖片,flash,音頻,視頻就成為瀏覽網(wǎng)頁的主流顯示,在手機上也一樣,本文將詳細介紹此功能的實現(xiàn)方法
    2012-11-11
  • Android開發(fā)筆記之:深入理解Cursor相關(guān)的性能問題

    Android開發(fā)筆記之:深入理解Cursor相關(guān)的性能問題

    本篇文章是對Android中Cursor相關(guān)的性能問題進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • Android開發(fā)筆記 TableLayout常用的屬性介紹

    Android開發(fā)筆記 TableLayout常用的屬性介紹

    今天看了安卓簡單控件的布局方式,大概有絕對、相對、表格、線性、幀式布局五種方式,表格布局里面的一些屬性相對來說比較復(fù)雜,下面主要談?wù)劚砀穹绞讲季值囊恍傩?/div> 2012-11-11
  • Flutter 剪裁組件的使用

    Flutter 剪裁組件的使用

    今天我們主要聊聊 Flutter 中的幾個剪裁組件的使用,也是項目當(dāng)中經(jīng)??梢杂玫降?,希望你可以有所收獲
    2021-06-06
  • Android處理時間各種方法匯總

    Android處理時間各種方法匯總

    這篇文章主要匯總了Android處理時間的各種方法,如何獲取當(dāng)前時間,日期之間的比較,如何計算兩段日期的重合日期等,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android dialog 去除虛擬按鍵的解決方法

    Android dialog 去除虛擬按鍵的解決方法

    今天小編就為大家分享一篇Android dialog 去除虛擬按鍵的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 快速解決Android適配底部返回鍵等虛擬鍵盤的問題

    快速解決Android適配底部返回鍵等虛擬鍵盤的問題

    今天小編就為大家分享一篇快速解決Android適配底部返回鍵等虛擬鍵盤的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Android中讓按鈕擁有返回鍵功能的方法及重寫返回鍵功能

    Android中讓按鈕擁有返回鍵功能的方法及重寫返回鍵功能

    這篇文章主要介紹了Android中讓按鈕擁有返回鍵功能的方法及重寫返回鍵功能,本文直接給出代碼寫法,并標(biāo)記了一些注意事項,需要的朋友可以參考下
    2015-04-04

最新評論