安卓Android碎片fragment實現靜態加載


靜態加載好后的界面如下,兩個碎片分別位於一個活動的左邊和右邊:

 

 

 左邊和右邊分別為一個碎片,這兩個碎片正好將一整個活動布滿。一個活動當中可以擁有多個碎片,碎片的含義就是可以在同一個UI界面下,將這個界面分成好幾個界面,並且可以分別更新自己的狀態,如果沒有碎片,那么如果你想要單獨在某一個區域實現活動的“跳轉”就不可能了,因此我們可以引入碎片,這樣就可以在這個區域單獨進行碎片的跳轉。在利用底部標題欄進行首頁UI的切換的時候就需要用到碎片,因此碎片在安卓開發當中十分廣泛,這篇博客將會與你講解如何實現靜態加載碎片,除了靜態加載碎片,還具有動態加載碎片的方式,兩種方式不同的方式都進行理解與引用,才可以把碎片的威力發揮到最大。下面是代碼,第一個是主活動當中的代碼,主活動一定得繼承Fragment這個類才可以實現碎片:

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

然后咱們創建碎片,在上述的UI界面當中有兩個碎片的區塊,因此我們連續創建兩個碎片:

二.MyFragment.java

我們在這個碎片當中利用Java直接引入TextView控件,當然在這個碎片所對應的xml文件當中也可以,這是相互等效的,都比較簡單。

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對象並返回

        //創建一個視圖對象, 設置數據並返回
        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對象並返回

        //創建一個視圖對象, 設置數據並返回
        TextView  textView = new TextView(getActivity());
        textView.setText("這是第二個碎片");
        textView.setBackgroundColor(Color.RED);

        return textView;
    }
}

之后在咱們的主活動的UI界面當中將代碼修改為:

四.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引入到咱們的主活動上面來啦,運行安卓項目,大功告成!!

如果想要在第二個碎片當中更改一下界面,那么咱們也可以直接在fragment的布局當中進行布局,而不是使用Java代碼來實現,畢竟在大多數時候,在實際的開發·過程當中,我們都會使用直接在碎片所對應的xml界面當中進行布局而不是用Java進行布。因為用Java布局實在是太難了,很難實現一些更加詳細同時華麗的效果。用碎片所對應的xml界面如下所示:

 

 

五.fragment_my_fragment2.xml

在主界面右邊的這個碎片是第二個碎片,因此只需要修改第二個碎片當中布局代碼為ImageView就好啦,修改后的代碼如下圖所示:

<?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">

<ImageView
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:src="@drawable/girl"
    />

</LinearLayout>

這樣就引入了一個ImageView,並在ImageView當中加入了一張外國美女的圖片。Preview當中顯示為:

 

 當然有了這個還不夠,之前我們在fragment所對應的Java代碼當中是返回的textview對象,這里我們已經對fragment進行了整體布局,因此應該返回一整個view對象,才可以將整個布局顯示出來,因此修改Java代碼為:

六.MyFragment2.java

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對象並返回

        //創建一個視圖對象, 設置數據並返回
       /* TextView  textView = new TextView(getActivity());
        textView.setText("這是第二個碎片");
        textView.setBackgroundColor(Color.RED);*/
       View view=inflater.inflate(R.layout.fragment_my_fragment2,container,false);
        return view;
    }
}

最后的界面運行結果如下:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM