Fragment碎片的使用
第一种:静态使用方法
left_fragment.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <? xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> < Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="button" /> </ LinearLayout > |
right_fragment.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <? xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:orientation="vertical"> < TextView android:text="This is right fragment" android:layout_gravity="center_horizontal" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </ LinearLayout > |
LeftFragment.java和 RightFragment.java 一样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.example.kaige.fragmenttest1; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class LeftFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fragment,container, false ); return view; } } |
activity_main.xml
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 | <? 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="horizontal" tools:context="com.example.kaige.fragmenttest1.MainActivity"> <!--静态碎片使用--> < fragment android:id="@+id/left_fragment" android:name="com.example.kaige.fragmenttest1.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> < fragment android:id="@+id/right_fragment" android:name="com.example.kaige.fragmenttest1.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" /> </ LinearLayout > |
MainActivity.java 不用改动
第二种:动态加载Fragment
修改activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <? 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="horizontal" tools:context="com.example.kaige.fragmenttest1.MainActivity"> <!--静态碎片使用--> < fragment android:id="@+id/left_fragment" android:name="com.example.kaige.fragmenttest1.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <!--动态添加fragment--> < FrameLayout android:id="@+id/right_fragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </ LinearLayout > |
添加
another_right_fragment.xml 和 AnotherRightFragment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <? xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffff00" android:orientation="vertical"> < TextView android:textSize="20sp" android:text="This is another right frament" android:layout_width="wrap_content" android:layout_gravity="center_horizontal" android:layout_height="wrap_content" /> </ LinearLayout > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.example.kaige.fragmenttest1; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * 动态添加fragment */ public class AnotherRightFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.another_right_fragment, container, false ); return view; } } |
最后修改MainActivity.java
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 | package com.example.kaige.fragmenttest1; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener( this ); replaceFragment( new RightFragment()); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: replaceFragment( new AnotherRightFragment()); break ; } } private void replaceFragment(Fragment fragment) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.replace(R.id.right_fragment, fragment); fragmentTransaction.commit(); } } |
运行看看效果吧..
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义