張萌&韓墨羽——Fragment 基礎與高級進階

Fragment基礎知識

在這裏插入圖片描述

Fragment 介紹

答:Fragment是Android3.0後引入的一個新的API,他出現的初衷是爲了適應大屏幕的平板電腦, 當然現在他仍然是平板APP UI設計的寵兒,而且我們普通手機開發也會加入這個Fragment, 我們可以把他看成一個小型的Activity,又稱Activity片段!想想,如果一個很大的界面,我們 就一個佈局,寫起界面來會有多麻煩,而且如果組件多的話是管理起來也很麻煩!而使用Fragment 我們可以把屏幕劃分成幾塊,然後進行分組,進行一個模塊化的管理!從而可以更加方便的在 運行過程中動態地更新Activity的用戶界面!另外Fragment並不能單獨使用,他需要嵌套在Activity 中使用,儘管他擁有自己的生命週期,但是還是會受到宿主Activity的生命週期的影響,比如Activity 被destory銷燬了,他也會跟着銷燬!。

Fragment爲什麼被稱爲第五大組件?

Fragment的使用次數是不輸於其他四大組件的,而且Fragment有自己的生命週期,比Activity更加節省內存。

Fragment的優勢有以下幾點:

模塊化(Modularity):我們不必把所有代碼全部寫在Activity中,而是把代碼寫在各自的Fragment中。
可重用(Reusability):多個Activity可以重用一個Fragment。
可適配(Adaptability):根據硬件的屏幕尺寸、屏幕方向,能夠方便地實現不同的佈局,這樣用戶體驗更好。

Fragment 應用

下圖是文檔中給出的一個Fragment分別對應手機與平板間不同情況的處理圖:
在這裏插入圖片描述
實際中,app中的應用
在這裏插入圖片描述
在這裏插入圖片描述

如何創建Fragment
創建Fragment對象:(右擊新建Fragment一步搞定)

內部執行的順序是:
(1).定義一個類, 繼承Fragment
(2).重寫父類的方法onCreateView()
(3).在onCreateView()方法中, 爲Fragment 創建UI界面

加載Fragment的兩種方式

靜態加載

在這裏插入圖片描述
在這裏插入圖片描述

自動生成的java文檔

public class MyFragment extends Fragment {
    public MyFragment() {
        // Required empty public constructor
    }
    /**
     * 參數詳解
     * fragment第一次創建用戶界面時回調的方法
     * @param inflater 實體加載器,用於加載一個fragment的視圖
     * @param container 表示當前fragment插入到activity中的對象
     * @param savedInstanceState  表示儲存一個fragment的信息
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }
}

自動生成的xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.MyFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

在需要加載fragment的activity中,想使用TextView一樣,直接創建一個fragment即可.
已下是MainActivity中的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:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!--一定要注意的是:name屬性是fragment的全限定名-->
    <fragment
        android:id="@+id/my_fragment_id"
        android:name="com.example.day004.fragment.MyFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </fragment>

</LinearLayout>

以上是靜態加載內容.

動態加載

在這裏插入圖片描述
動態加載也就是在java代碼中創建fragment.

FragmentManager 介紹

1.簡介
中文名稱:碎片管理器
出生日期:Android 3.0/API level 11
助 理:FragmentTransaction
獲取方式:
①Android 3.0前的版本使用getSupportFragmentManager()方法獲取
②Android 3.0之後的版本用getFragmentManager()獲取.

爲了兼容的效果,我們選擇getSupportFragmentManager()方法.
getFragmentManager()方法已經廢棄.

一定要注意,如果用getSupportFragmentManager()這個方法,就整個項目全部用,
不要在項目裏交叉使用getFragmentManager()方法.
否則你得請神仙來找錯了.

2.實現流程
1:獲得FragmentManager對象
FragmentManager fragmentManager=getSupportFragmentManager();
2:開啓事務
FragmentTransaction transaction = fragmentManager.beginTransaction();
3:通過FragmentTransaction 調用add()、replace()方法管理fragment
4:transaction .commit();

在這裏插入圖片描述

package com.example.day004;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.day004.fragment.OneFragment;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1,創建fragment的管理對象
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        //2,獲取fragment的事物對象,並開啓事務
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        //3,調用事務中相應的方法,來操作fragment
        //add方法參數,第一個要放入的容器(佈局的Id),第二個是fragment對象
        fragmentTransaction.add(R.id.main_layout_id,new MyFragment());
        //4,提交事務
        fragmentTransaction.commit();
    }
}

add,remove,replace,hide 方法

package com.example.day004;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.day004.fragment.MyFragment;
import com.example.day004.fragment.OneFragment;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1,創建fragment的管理對象
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        //2,獲取fragment的事物對象,並開啓事務
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        //3,調用事務中相應的方法,來操作fragment
        //add方法參數,第一個要放入的容器(佈局的Id),第二個是fragment對象
         MyFragment myFragment = new MyFragment();
        fragmentTransaction.add(R.id.main_layout_id,myFragment);
        //移除一個Fragment
        fragmentTransaction.remove(myFragment);
        OneFragment oneFragment = new OneFragment(); //實例化一個Fragment對象
        //replace(替換一個佈局)執行過程是先 remove 然後在 add.
        fragmentTransaction.replace(R.id.main_layout_id,oneFragment);
        //隱藏一個Fragment
        fragmentTransaction.hide(oneFragment);
        //4,提交事務
        fragmentTransaction.commit();

    }
}

Fragment的生命週期

在這裏插入圖片描述

文字描述版本的生命週期.

1.onAttach() :Fragment與Activity有聯繫。
2.onCreate():創建Fragment
3.onCreateView():創建Fragment視圖,儘量不要做耗時操作
4.onActivityCreated():當Activity中的onCreate方法執行完後調用。
5.onStart():啓動。
6.onResume():可見
7.onPause():不可見
8.onStop():停止。
9. onDestroyView() :銷燬Fragment視圖
10.onDestroy():銷燬fragment對象
11.onDetach():Fragment和Activity解除關聯的時候調用
在這裏插入圖片描述

Fragment高級進階

Fragment 回退棧

radioGroupId.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.person_id:
                        manager = getSupportFragmentManager();
                        fragmentTransaction = manager.beginTransaction();
//                        fragmentTransaction.replace(R.id.frame_layout_id,new FristFragment());
                        fragmentTransaction.add(R.id.frame_layout_id,new FristFragment());
						//入棧
                        fragmentTransaction.addToBackStack("11");
                        fragmentTransaction.commit();
                        break;
                    case R.id.message_id:
                        manager = getSupportFragmentManager();
						
						//出棧
                        manager.popBackStack(); 
                        SelectActivity.this.fragmentTransaction = manager.beginTransaction();
                        SelectActivity.this.fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
//                        fragmentTransaction.addToBackStack(null);
                        SelectActivity.this.fragmentTransaction.commit();
                        break;

                    default:
                        break;
                }
            }
        });
         	case R.id.message_id:
manager = getSupportFragmentManager();

//  manager.popBackStack();
    fragmentTransaction = manager.beginTransaction();
    fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
    //添加了返回棧,點擊back的時候,不會退出activity,而會返回到之前的fragment界面.
//  fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
     break;

Fragment 傳值介紹

activity 給 fragment傳值
主要涉及到一個方法是getArguments()和setArguments().
一個設置屬性值,一個去取屬性值.

步驟:
要傳的值,放到bundle對象裏;
在Activity中創建該Fragment的對象fragment,通過調用
fragment.setArguments()傳遞到fragment中;
然後更新fragment.
在該Fragment中通過調用getArguments()得到bundle對象,就能得到裏面的值。

代碼來了 這是剛創建完成的activity

package com.example.day004;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

/**
 * activity 向 fragment 傳值
 */
public class A2FActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_2_f);
    }

	//點擊事件的方法
	public void click(View view) {
	}
}

這是修完以後的他的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="vertical"
    tools:context=".A2FActivity">

    <EditText
        android:id="@+id/et_id"
        android:hint="來啊,傳點啥吧!"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

   <!--在activity中定義了點擊事件-->
    <Button
        android:id="@+id/button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="點我試試"
        android:onClick="click"
        />

    <!--用來顯示內容的,是個容器,裏面可以放fragment-->
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/linear_layout_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

</LinearLayout>

創建fragment(右鍵一鍵完事,帶佈局)

package com.example.day004.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.day004.R;

/**
  *用來接受傳值的
 */
public class ShowContextFragment extends Fragment {
    
    public ShowContextFragment() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_show_context, container, false);
    }
}

修改後的fragment的xml佈局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.ShowContextFragment">
    
    <!--只加了一個Id,其他可有可無-->
    <TextView
        android:id="@+id/context_tv_id"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/colorAccent"
        android:textSize="30sp"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

完整版的fragment

package com.example.day004.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.day004.R;

/**
 *用來接受傳值的
 */
public class ShowContextFragment extends Fragment {

    private TextView textView;
    public ShowContextFragment() {
        // Required empty public constructor
    }
    //現在text裏面的值是"hello_blank_fragment",思路就是拿到控件,重新賦值即可.
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_show_context, container, false);
        //1 拿控件
        textView = inflate.findViewById(R.id.context_tv_id);
        //2 給控件賦值
        Bundle arguments = getArguments();
        if(arguments != null){ //第一次啓動一定爲null,所以要判斷一下
            String key = arguments.getString("key");
            textView.setText(key);
        }
        return inflate;
    }

}

完整版的a2fActivity

package com.example.day004;

import android.icu.util.BuddhistCalendar;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.example.day004.fragment.ShowContextFragment;

import java.io.BufferedReader;

/**
 * activity 向 fragment 傳值
 */
public class A2FActivity extends AppCompatActivity {
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a_2_f);

        //0,取到edit的控件和值
        editText =  findViewById(R.id.et_id);

        //1,在這裏動態添加一個fragment
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        //注意這個佈局文件,是R.layout.activity_a_2_f xml文件裏的線性佈局
        fragmentTransaction.add(R.id.linear_layout_id,new ShowContextFragment());
        fragmentTransaction.commit();
    }

    public void click(View view) {
        //取到輸入的值
        String string = editText.getText().toString();
        //創建fragment對象
        ShowContextFragment showContextFragment = new ShowContextFragment();
        //創建bundle
        Bundle bundle = new Bundle();
        bundle.putString("key",string);
        //給fragment對象賦值
        showContextFragment.setArguments(bundle);

        //動態修改fragment
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.linear_layout_id,showContextFragment);
        fragmentTransaction.commit();
    }
}

fragment 給 activity傳值

第一種:
在Activity中調用getFragmentManager()得到fragmentManager,,調用findFragmentByTag(tag)或者通過findFragmentById(id)
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);

第二種:
通過回調的方式,定義一個接口(可以在Fragment類中定義),接口中有一個空的方法,在fragment中需要的時候調用接口的方法,值可以作爲參數放在這個方法中,然後讓Activity實現這個接口,必然會重寫這個方法,這樣值就傳到了Activity中.
大白話,就是java的父類引用指向了子類對象

Activity中的代碼

package com.example.day004;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.day004.fragment.ShowTitleFragment;

public class F2AActivity extends AppCompatActivity implements ShowTitleFragment.MyListener {

    private TextView textView;
    @Override
    public void sendMessage(String string) {
        textView.setText(string);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_f_2_a);
        textView = findViewById(R.id.f2a_tv_id);
    }
}

Activity中的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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".F2AActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/f2a_tv_id"
        android:textSize="30sp"
        android:hint="提示而已"
        />

    <fragment
        android:name="com.example.day004.fragment.ShowTitleFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/f2a_fm_id">

    </fragment>

</LinearLayout>

fragment中的代碼

package com.example.day004.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.day004.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShowTitleFragment extends Fragment {

    private EditText editText;
    private Button button;

    private MyListener listener;

    public ShowTitleFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        //拿到與當前fragment關聯的Activity.
        listener = (MyListener) getActivity(); 
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_show_title, container, false);

       //取到所有控件
        editText = inflate.findViewById(R.id.fm_title_et_id);
        button = inflate.findViewById(R.id.fm_title_button_id);
        //點擊事件
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = editText.getText().toString();
                if(string != null){
                    //注意了.這裏是父類引用指向了子類對象,其實是activity中的sendmessage方法在執行.
                    listener.sendMessage(string);
                }
            }
        });
		//返回fragment中的佈局視圖
        return inflate;
    }

   //自定義的接口
    public interface MyListener{
        void sendMessage(String string);
    }

}

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"
    android:orientation="vertical"
    tools:context=".fragment.ShowTitleFragment">
    
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fm_title_et_id"
        android:hint="整的啥吧"
        />

    <Button
        android:id="@+id/fm_title_button_id"
        android:text="發送了啊"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

fragment 給 fragment 傳值

第一種:
動態創建的fragment通過findFragmentByTag得到另一個的Fragment的對象,這樣就可以調用另一個的方法了。
靜態創建的fragment通過findFragmentById得到另一個的Fragment的對象,這樣就可以調用另一個的方法了。

package com.example.day005.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.day005.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class LeftFragment extends Fragment {

    private EditText editText;
    private Button send_button;

    public LeftFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_left, container, false);
        editText = inflate.findViewById(R.id.left_textView_id);
        send_button = inflate.findViewById(R.id.left_button_id);

        send_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = editText.getText().toString();
                
                **瞭解一種即可,不重點**
                //1 fragment傳值方式1
                // 通過id找的指定fragment,調用他的方法,給他的組件賦值.
                FragmentManager fragmentManager = getFragmentManager(); //注意,這個manager是v4包下的.
                RightFragment rightFragment=(RightFragment) fragmentManager.findFragmentById(R.id.right_fragment_id);
                rightFragment.setTextView(string);

                //2 通過id找的指定fragment,得到他所有的view.在通過findViewById 找到組件.賦值
                TextView textView = getFragmentManager().findFragmentById(R.id.right_fragment_id).getView().findViewById(R.id.right_textview_id);
                textView.setText(string);

                //3
                //因爲兩個fragment在一個activity裏面,所有可以通過拿到activity裏面所有的指定組件.
                TextView textView = (TextView) getActivity().findViewById(R.id.right_textview_id);
                textView.setText(string);

            }
        });
        return inflate;
    }
}

第二種(重點)

通過接口回調的方式。
與上面的接口回調用法相同

第三種:
通過setArguments,getArguments的方式。

fragment 多層嵌套
與正常使用區別不大

 FragmentManager childFragmentManager = getChildFragmentManager();
 FragmentTransaction fragmentTransaction = childFragmentManager.beginTransaction();
 fragmentTransaction.add(R.id.left_fragment_id,new RightFragment());
 fragmentTransaction.commit();

案例(微信底部按鈕與fragment實現界面切換)

activity中的代碼

package com.example.day004.activities;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.day004.R;
import com.example.day004.fragment.FristFragment;
import com.example.day004.fragment.SecondFragment;

/**
 * 底部導航
 */
public class SelectActivity extends AppCompatActivity {
    private FrameLayout frameLayoutId;
    private RadioGroup radioGroupId;
    private RadioButton personId;
    private RadioButton messageId;
    private static final String TAG = "SelectActivity";
    private FragmentManager manager;
    private FragmentTransaction fragmentTransaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);

        frameLayoutId = findViewById(R.id.frame_layout_id);
        radioGroupId = findViewById(R.id.radio_group_id);

        radioGroupId.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.person_id:
                        manager = getSupportFragmentManager();
                        fragmentTransaction = manager.beginTransaction();
//                        fragmentTransaction.replace(R.id.frame_layout_id,new FristFragment());
                        fragmentTransaction.add(R.id.frame_layout_id,new FristFragment());
                        fragmentTransaction.addToBackStack("11");
                        Log.i(TAG, "onCheckedChanged: "+SelectActivity.class.getName());
                        fragmentTransaction.commit();
                        break;
                    case R.id.message_id:
                        manager = getSupportFragmentManager();
                        manager.popBackStack();
                        SelectActivity.this.fragmentTransaction = manager.beginTransaction();
                        SelectActivity.this.fragmentTransaction.replace(R.id.frame_layout_id,new SecondFragment());
//                        fragmentTransaction.addToBackStack(null);
                        SelectActivity.this.fragmentTransaction.commit();
                        break;
                    default:
                        break;
                }
            }
        });
    }
}

activity的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="vertical"
    tools:context=".activities.SelectActivity">

    <FrameLayout
        android:id="@+id/frame_layout_id"
        android:layout_weight="9"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </FrameLayout>

    <RadioGroup
        android:id="@+id/radio_group_id"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <RadioButton
            android:layout_weight="1"
            android:id="@+id/person_id"
            android:button="@null"
            android:text="聯繫人"
            android:gravity="center"
            android:checked="true"
            android:drawableTop="@drawable/select_pserson"
            android:textColor="@drawable/select_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />

        <RadioButton
            android:layout_weight="1"
            android:id="@+id/message_id"
            android:button="@null"
            android:gravity="center"
            android:drawableTop="@drawable/select_message"
            android:textColor="@drawable/select_text"
            android:text="信息"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            />
    </RadioGroup>

</LinearLayout>

說明

android:drawableTop="@drawable/select_message"
android:textColor="@drawable/select_text"
這兩個屬性,參照下面鏈接創建
https://blog.csdn.net/qq_34178710/article/details/91411003
java代碼中的fragment,右鍵創建即可.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章