Fragment 静态与动态注册

82 篇文章 1 订阅

Fragment

静态注册

  • 主布局、Fragment布局、主函数、Fragment类

主布局

将Fregment布局声明出来

<?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=".MainActivity">

    <fragment
        android:id="@+id/fragment_static"
        android:name="com.example.fragment.StaticFragment"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是每个界面的具体内容!"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</LinearLayout>

Fragment布局

主布局中声明的碎片,也就是Fragment内部的布局方式。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#bbffbb">
    <TextView
        android:id="@+id/tv_adv"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="广告图片"
        android:textColor="#000000"
        android:textSize="17sp" />
    <ImageView
        android:id="@+id/iv_adv"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:src="@drawable/adv"
        android:scaleType="fitCenter" />
</LinearLayout>

主函数

将Fragment实例化出来。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Fragment类

返回Fregment对应的视图对象

public class StaticFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "StaticFragment";
    protected View mView; // 声明一个视图对象
    protected Context mContext; // 声明一个上下文对象

    // 创建碎片视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        mContext = getActivity(); // 获取活动页面的上下文
//        // 根据布局文件fragment_static.xml生成视图对象
//        mView = inflater.inflate(R.layout.fragment_static, container, false);
//        TextView tv_adv = mView.findViewById(R.id.tv_adv);
//        ImageView iv_adv = mView.findViewById(R.id.iv_adv);
//        tv_adv.setOnClickListener(this); // 设置点击监听器
//        iv_adv.setOnClickListener(this); // 设置点击监听器
//        Log.d(TAG, "onCreateView");
//        return mView; // 返回该碎片的视图对象
        return inflater.inflate(R.layout.fragment_static, container, false); // 返回该碎片的视图对象
    }

生命周期

public class StaticFragment extends Fragment implements View.OnClickListener {
    private static final String TAG = "fragment";
    protected View mView; // 声明一个视图对象
    protected Context mContext; // 声明一个上下文对象

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.e(TAG, "onAttach...");
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate...");
    }

    // 创建碎片视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.e(TAG, "onCreateView...");
//        mContext = getActivity(); // 获取活动页面的上下文
//        // 根据布局文件fragment_static.xml生成视图对象
//        mView = inflater.inflate(R.layout.fragment_static, container, false);
//        TextView tv_adv = mView.findViewById(R.id.tv_adv);
//        ImageView iv_adv = mView.findViewById(R.id.iv_adv);
//        tv_adv.setOnClickListener(this); // 设置点击监听器
//        iv_adv.setOnClickListener(this); // 设置点击监听器
//        Log.d(TAG, "onCreateView");
//        return mView; // 返回该碎片的视图对象
        return inflater.inflate(R.layout.fragment_static, container, false); // 返回该碎片的视图对象
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Log.e(TAG, "onViewCreated....");
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onStart() {
        Log.e(TAG, "onStart...");
        super.onStart();
    }

    @Override
    public void onResume() {
        Log.e(TAG, "onResume...");
        super.onResume();
    }

    @Override
    public void onClick(View v) {

    }
}
2022-07-03 19:41:41.817 1809-1809/com.example.fragment E/fragment: onAttach...
2022-07-03 19:41:41.818 1809-1809/com.example.fragment E/fragment: onCreate...
2022-07-03 19:41:41.818 1809-1809/com.example.fragment E/fragment: onCreateView...
2022-07-03 19:41:41.825 1809-1809/com.example.fragment E/fragment: onViewCreated....
2022-07-03 19:41:41.832 1809-1809/com.example.fragment E/fragment: onStart...
2022-07-03 19:41:41.836 1809-1809/com.example.fragment E/fragment: onResume...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学知识拯救世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值