43 Android fragment 回退栈
来源:互联网 发布:mysql 最近3天 编辑:程序博客网 时间:2024/06/07 04:45
layout文件夹下代码
f1.xml(被加载到fragment上)
<?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:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件管理" android:layout_marginTop="100dp" android:layout_marginLeft="20dp" android:textSize="25sp" /></LinearLayout>
f2.xml(被加载到fragment上)
<?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:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户管理" android:layout_marginTop="100dp" android:layout_marginLeft="20dp" android:textSize="25sp" /></LinearLayout>
f3.xml(被加载到fragment上)
<?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:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="产品管理" android:layout_marginTop="100dp" android:layout_marginLeft="20dp" android:textSize="25sp" /></LinearLayout>
f4.xml(被加载到fragment上)
<?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:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="资源管理" android:layout_marginTop="100dp" android:layout_marginLeft="20dp" android:textSize="25sp" /></LinearLayout>
Fragment1 extends fragment 其余三个和他相同
Fragment1.java 代码
package com.example.android_fragment_backstack;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment {@Overridepublic void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stubView view=inflater.inflate(R.layout.f1, null);return view;}@Overridepublic void onPause() {// TODO Auto-generated method stubsuper.onPause();}}
activity_main.xml 代码文件(主要是两个布局)
<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=".MainActivity" > <LinearLayout android:id="@+id/left" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" android:background="#cccccc" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件管理" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户管理" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="产品管理" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="资源管理" /> </LinearLayout> <LinearLayout android:layout_weight="3" android:id="@+id/center" android:background="#0fffff" android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout></LinearLayout>
MainActivity.java代码
package com.example.android_fragment_backstack;import android.os.Bundle;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {private FragmentManager manager;private FragmentTransaction transaction;private Button button1,button2,button3,button4;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1=(Button)findViewById(R.id.button1);button2=(Button)findViewById(R.id.button2);button3=(Button)findViewById(R.id.button3);button4=(Button)findViewById(R.id.button4);button1.setOnClickListener(this);button2.setOnClickListener(this);button3.setOnClickListener(this);button4.setOnClickListener(this);manager = this.getFragmentManager();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtransaction = manager.beginTransaction();switch (v.getId()) {case R.id.button1:Fragment1 f1=new Fragment1();transaction.replace(R.id.center, f1,"fragment1");transaction.addToBackStack("fragment1");//添加到Activity管理的回退栈中break;case R.id.button2:Fragment2 f2=new Fragment2();transaction.replace(R.id.center, f2,"fragment2");transaction.addToBackStack("fragment2");//添加到Activity管理的回退栈中break;case R.id.button3:Fragment3 f3=new Fragment3();transaction.replace(R.id.center, f3,"fragment3");transaction.addToBackStack("fragment3");//添加到Activity管理的回退栈中break;case R.id.button4:Fragment4 f4=new Fragment4();transaction.replace(R.id.center, f4,"fragment4");transaction.addToBackStack("fragment4");//添加到Activity管理的回退栈中break;default:break;}transaction.commit();}}
0 0
- 43 Android fragment 回退栈
- Android中的Fragment回退栈
- android fragment的回退栈
- Android之Fragment回退栈详解
- Android开发之Fragment回退栈
- android Fragment回退栈的问题
- Android Fragment回退栈的使用
- Android开发之Fragment回退栈
- Android Fragment---创建Fragment
- Android Fragment---管理Fragment
- Android Fragment---创建Fragment
- Android Fragment---管理Fragment
- android Fragment生命周期和回退栈学习以及Fragment之间通信
- (Android) Fragment
- Android Fragment
- Android fragment
- Android Fragment
- Android Fragment
- android-检测耳机的插入和拔出动作
- (1)回环矩阵
- k近邻分类算法(kNN)
- 拓胜第二十九天
- ASP.NET 管理网站(虚拟目录)
- 43 Android fragment 回退栈
- VS2010MFC创建线程
- html5中关于canvas标签用法(绘图)
- (转)理解(Memory Reordering)从JVM并发看CPU内存指令重排序
- 事件处理
- 《轻松搞定网页设计》学习笔记系列之一
- python dict排序之一
- Head First Java_Study Notes_Chapter 1 Break the Surface
- 树莓派学习笔记——I2C设备载入和速率设置