Fragement的使用
来源:互联网 发布:woc什么意思网络用语 编辑:程序博客网 时间:2024/05/14 05:59
fragment分为动态加载和静态加载
静态加载的方法
布局文件 main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <RadioGroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:orientation="horizontal" > <span style="color:#ff0000;"> <RadioButton android:id="@+id/first" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/radio_pressed" android:button="@null" android:drawableTop="@drawable/ic_launcher" android:gravity="center_horizontal" android:text="静态加载" /></span> <RadioButton android:id="@+id/second" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/radio_pressed" android:button="@null" android:drawableTop="@drawable/ic_launcher" android:gravity="center_horizontal" android:text="动态加载" /> <RadioButton android:id="@+id/thrid" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/radio_pressed" android:button="@null" android:drawableTop="@drawable/ic_launcher" android:gravity="center_horizontal" android:text="生命周期" /> <RadioButton android:id="@+id/fourth" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/radio_pressed" android:button="@null" android:drawableTop="@drawable/ic_launcher" android:gravity="center_horizontal" android:text="传值通信" /> </RadioGroup></RelativeLayout>对应Java文件
MainActivity.java
package com.example.android_fragment;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.content.Intent;import android.os.Bundle;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends Activity implements OnCheckedChangeListener {private RadioGroup group;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main);group = (RadioGroup) findViewById(R.id.radiogroup);group.setOnCheckedChangeListener(this);}@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubswitch (checkedId) {case R.id.first: { Intent intent=new Intent(this,MainActivity2.class); startActivity(intent);break;}case R.id.second: { MyFragment2 fragment2=new MyFragment2(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); beginTransaction.add(R.id.frame, fragment2); beginTransaction.addToBackStack(null); beginTransaction.commit();break;}case R.id.thrid: { Intent intent=new Intent(MainActivity.this,MainActivity3.class); startActivity(intent);break;}case R.id.fourth: {Intent intent=new Intent(MainActivity.this,MainActivity4.class); startActivity(intent);break; }}}}
关系图
MainActivity2
package com.example.android_fragment;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity2 extends Activity{private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.main2);Button button=(Button) findViewById(R.id.button);tv=(TextView) findViewById(R.id.text);button.setText("改变");button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtv.setText("TextView改变了");}});}}
main2.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" > <fragment android:id="@+id/fragment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.example.android_fragment.MyFragment" /></LinearLayout>
package com.example.android_fragment;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.Button;import android.widget.TextView;import android.widget.Toast;public class MyFragment extends Fragment{private String aaa;public String getAaa() {return aaa;}public void setAaa(String aaa) {this.aaa = aaa;}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// TODO Auto-generated method stub//layout布局文件转换成View对象/** * resource:Fragment需要加载的布局文件 * root:加载layout的父ViewGroup * attactToRoot:false,不返回父ViewGroup */View view = inflater.inflate(R.layout.fragment, container, false);TextView text=(TextView) view.findViewById(R.id.text);Button button=(Button) view.findViewById(R.id.button);text.setText("静态加载Fragment");button.setText("获取内容");button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString value = getAaa();Toast.makeText(getActivity(), "value="+value, Toast.LENGTH_SHORT).show();}});return view;}}
<?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/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
0 0
- Fragement的使用
- Fragement的使用
- Fragement的使用
- Fragement使用
- 一个使用Fragement控件的简单实例
- Android Fragement学习笔记(三)----PreferenceFragment的使用
- Android Fragement学习笔记(三)----PreferenceFragment的使用
- Android Fragement学习笔记(三)----PreferenceFragment的使用
- 【Android进阶】Activity和Fragement中onSaveInstanceState()的使用详解
- Android Fragement学习笔记(三)----PreferenceFragment的使用
- Activity和Fragement中onSaveInstanceState()的使用详解
- Android开发之Fragement框架层的使用
- 处理fragement的生命周期
- 处理fragement的生命周期
- fragement的生命周期
- fragement的生命周期
- 处理fragement的生命周期
- Fragement真正的解析
- jquery 笔记之事件
- hdu1260 Tickets(动态规划)
- 在eclipse中配置JnetPCAP,Windows环境
- en-win7-x64-sp1 + vs2015.3 + Lenovo Z470 装机&镜像
- GET和POST之间的区别
- Fragement的使用
- mybatis小记
- c++ 学习之路 第四天 引用
- Map
- (十八)、继承
- MySQL case when then else end 用法
- 浅谈Block与代理
- HDOJ 2084 数塔
- confuse & task