Android Fragment(一):基本使用
三、
四、 代码
4.1、activity_main.xml
<?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:gravity="center" android:orientation="horizontal" > <!-- 静态加载fragment:把fragment当成一个ui标签使用 --> <fragment android:id="@+id/first" android:name="com.gatsby.fragmentone.FirstFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <fragment android:id="@+id/second" android:name="com.gatsby.fragmentone.SecondFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout>
4.2、fragment_first.xml
<?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" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="FirstFragment" android:textSize="64sp" android:gravity="center"/> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image1" /> </LinearLayout>
4.3、fragment_second.xml
<?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" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="SecondFragment" android:textSize="64sp" android:gravity="center"/> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image8" /> </LinearLayout>
4.5、MaInActivity.java
package com.gatsby.fragmentone; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
4.6、FirstFragment.java
package com.gatsby.fragmentone; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FirstFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_first, container, false); return view; } }
4.7、SecondFragment.java
package com.gatsby.fragmentone; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class SecondFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_second, container, false); return view; } }
五、
5.1、activity_main.xml
<?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:gravity="center" android:orientation="vertical" > <LinearLayout android:id="@+id/fragment_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FragmentFirst" android:textSize="16sp" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FragmentSecond" android:textSize="16sp" /> </LinearLayout>
5.2、fragment_first.xml
<?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" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="FirstFragment" android:textSize="16sp" /> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image12"/> </LinearLayout> </LinearLayout>
5.3、fragment_second.xml
<?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" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="SecondFragment" android:textSize="16sp" /> <ImageView android:id="@+id/imageView2" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image9" /> </LinearLayout> </LinearLayout>
5.4、MainActivity.java
package com.gatsby.fragmenttwo; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { Button btn1, btn2; FragmentManager fragmentManager;// Fragment 管理器 FragmentTransaction fragmentTransaction; // Fragment 事务处理 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn1.setOnClickListener(this); btn2.setOnClickListener(this); fragmentManager = getFragmentManager(); // 得到Fragment 管理器对象 fragmentTransaction = fragmentManager.beginTransaction(); // 开始fragmnet 的事务处理 FirstFragment firstfragment = new FirstFragment(); fragmentTransaction.add(R.id.fragment_id, firstfragment); // fragment_id 是布局中给fragment 占位置的控 fragmentTransaction.commit(); // 提交事务 } @Override public void onClick(View v) { fragmentManager = getFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); switch (v.getId()) { case R.id.btn1: fragmentTransaction.replace(R.id.fragment_id, new FirstFragment()); fragmentTransaction.addToBackStack(null); break; case R.id.btn2: fragmentTransaction.replace(R.id.fragment_id, new SecondFragment()); fragmentTransaction.addToBackStack(null); break; } fragmentTransaction.commit(); } }
5.5、FirstFragment.java
package com.gatsby.fragmenttwo; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class FirstFragment extends Fragment implements OnClickListener { TextView textView1; ImageView imageView1; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_first, container, false); textView1 = (TextView) view.findViewById(R.id.textView1); imageView1 = (ImageView) view.findViewById(R.id.imageView1); imageView1.setOnClickListener(this); return view; } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.imageView1: textView1.setText("you onClick FragmentFirst!"); imageView1.setImageResource(R.drawable.image23); break; default: break; } } }
5.6、SecondFragment.java
package com.gatsby.fragmenttwo; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class SecondFragment extends Fragment implements OnClickListener { TextView textView2; ImageView imageView2; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.fragment_second, container, false); textView2 = (TextView) view.findViewById(R.id.textView2); imageView2 = (ImageView) view.findViewById(R.id.imageView2); imageView2.setOnClickListener(this); return view; } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.imageView2: textView2.setText("you onClick FragmentSecond!"); imageView2.setImageResource(R.drawable.image17); break; default: break; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】