二十四、Fragment(静态添加FragMent)
1. 具备生命周期(很像一个子activity)
2. 必须委托在activity中才能运行
一、实现静态添加 FragMent
1.右键包=》NEW => FragMent (选择对应的fragment)
2.编辑页面展示代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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= ".BlankFragment1" > <TextView android:id= "@+id/textview" android:layout_width= "match_parent" android:layout_height= "40dp" android:text= "@string/hello_blank_fragment" /> <Button android:id= "@+id/btn" android:layout_width= "match_parent" android:layout_height= "40dp" android:text= "how are you 1" /> </LinearLayout> |
3.编辑 “ BlankFragment1 ” 类的相关方法,如下图所示:在 onCreateView 方法中,进行点击事件的绑定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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; import android.widget.Button; import android.widget.TextView; public class BlankFragment1 extends Fragment { private View root; private TextView textview; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (root== null ){ root = inflater.inflate(R.layout.fragment_blank1,container, false ); } textview = root.findViewById(R.id.textview); button=root.findViewById(R.id.btn); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { textview.setText( "Yes ,I am fine 1" ); } }); return root; } } |
4.主程序的调用(只需要在页面中创建执行标签就可以)
注意:1. 必须设定 id 属性的值
2. name属性跟随需要绑定的 FragMent 页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?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= ".MainActivity" > <!--fragment 设置绑定--> <fragment android:id= "@+id/fragment1" android:name= "com.example.myapplication.BlankFragment1" android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_weight= "1" /> <fragment android:name= "com.example.myapplication.BlankFragment2" android:layout_width= "match_parent" android:layout_height= "match_parent" android:layout_weight= "1" android:id= "@+id/fragment2" /> </LinearLayout> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现