Android碎片fragment實現(xiàn)靜態(tài)加載的實例代碼
靜態(tài)加載好后的界面如下,兩個碎片分別位于一個活動的左邊和右邊:
左邊和右邊分別為一個碎片,這兩個碎片正好將一整個活動布滿。一個活動當(dāng)中可以擁有多個碎片,碎片的含義就是可以在同一個UI界面下,將這個界面分成好幾個界面,并且可以分別更新自己的狀態(tài),如果沒有碎片,那么如果你想要單獨在某一個區(qū)域?qū)崿F(xiàn)活動的“跳轉(zhuǎn)”就不可能了,因此我們可以引入碎片,這樣就可以在這個區(qū)域單獨進行碎片的跳轉(zhuǎn)。在利用底部標題欄進行首頁UI的切換的時候就需要用到碎片,因此碎片在安卓開發(fā)當(dāng)中十分廣泛,這篇博客將會與你講解如何實現(xiàn)靜態(tài)加載碎片,除了靜態(tài)加載碎片,還具有動態(tài)加載碎片的方式,兩種方式不同的方式都進行理解與引用,才可以把碎片的威力發(fā)揮到最大。下面是代碼,第一個是主活動當(dāng)中的代碼,主活動一定得繼承Fragment這個類才可以實現(xiàn)碎片:
一.MainActivity.java
import androidx.fragment.app.FragmentActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends FragmentActivity { public MainActivity() { Log.e("TAG", "MainActivity().."); } @Override protected void onCreate(Bundle savedInstanceState) { Log.e("TAG", "MainActivity onCreate().."); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
然后咱們創(chuàng)建碎片,在上述的UI界面當(dāng)中有兩個碎片的區(qū)塊,因此我們連續(xù)創(chuàng)建兩個碎片:
二.MyFragment.java
我們在這個碎片當(dāng)中利用Java直接引入TextView控件,當(dāng)然在這個碎片所對應(yīng)的xml文件當(dāng)中也可以,這是相互等效的,都比較簡單。
import android.graphics.Color; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加載布局得到View對象并返回 //創(chuàng)建一個視圖對象, 設(shè)置數(shù)據(jù)并返回 TextView textView = new TextView(getActivity()); textView.setText("這是第一個碎片"); textView.setBackgroundColor(Color.RED); return textView; } }
三.MyFragment2.java
import android.graphics.Color; import android.os.Bundle; import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MyFragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加載布局得到View對象并返回 //創(chuàng)建一個視圖對象, 設(shè)置數(shù)據(jù)并返回 TextView textView = new TextView(getActivity()); textView.setText("這是第二個碎片"); textView.setBackgroundColor(Color.RED); return textView; } }
之后在咱們的主活動的UI界面當(dāng)中將代碼修改為:
四.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".MainActivity"> <fragment android:name="com.example.fragment.MyFragment" android:id="@+id/myfragment_1" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> <fragment android:name="com.example.fragment.MyFragment2" android:id="@+id/myfragment_2" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
這樣就可以把fragment引入到咱們的主活動上面來啦,運行安卓項目,大功告成?。?/p>
總結(jié)
以上所述是小編給大家介紹的在Android碎片fragment實現(xiàn)靜態(tài)加載的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Android 利用反射+try catch實現(xiàn)sdk按需引入依賴庫的方法
這篇文章主要介紹了Android 利用反射+try catch來實現(xiàn)sdk按需引入依賴庫,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Android編程實現(xiàn)自定義PopupMenu樣式示例【顯示圖標與設(shè)置RadioButton圖標】
這篇文章主要介紹了Android編程實現(xiàn)自定義PopupMenu樣式功能,結(jié)合實例形式分析了Android顯示圖標與設(shè)置RadioButton圖標相關(guān)操作技巧,需要的朋友可以參考下2017-01-01Android錄音--AudioRecord、MediaRecorder的使用
本篇文章主要介紹了Android錄音--AudioRecord、MediaRecorder的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02