首页 > 代码库 > Android学习笔记之fragment的静态加载和动态加载
Android学习笔记之fragment的静态加载和动态加载
1.xml布局文件:
main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" > 4 5 <LinearLayout 6 android:id="@+id/linearLayout" 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:orientation="vertical"></LinearLayout>10 11 12 <RadioGroup 13 android:id="@+id/radioGroup"14 android:layout_width="match_parent"15 android:layout_height="wrap_content"16 android:orientation="horizontal"17 android:layout_alignParentBottom="true"18 >19 20 <RadioButton21 android:id="@+id/first"22 android:layout_width="0dp"23 android:layout_height="wrap_content"24 android:layout_weight="1"25 26 android:button="@null"27 android:drawableTop="@drawable/ic_launcher"28 android:gravity="center_horizontal"29 android:text="静态加载" />30 31 <RadioButton32 android:id="@+id/secnod"33 android:layout_width="0dp"34 android:layout_height="wrap_content"35 android:layout_weight="1"36 37 android:button="@null"38 android:drawableTop="@drawable/ic_launcher"39 android:gravity="center_horizontal"40 android:text="动态加载" />41 42 <RadioButton43 android:id="@+id/three"44 android:layout_width="0dp"45 android:layout_height="wrap_content"46 android:layout_weight="1"47 48 android:button="@null"49 android:drawableTop="@drawable/ic_launcher"50 android:gravity="center_horizontal"51 android:text="静态加载" />52 53 <RadioButton54 android:id="@+id/four"55 android:layout_width="0dp"56 android:layout_height="wrap_content"57 android:layout_weight="1"58 59 android:button="@null"60 android:drawableTop="@drawable/ic_launcher"61 android:gravity="center_horizontal"62 android:text="静态加载" />63 </RadioGroup>64 65 </RelativeLayout>
main2.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 6 <fragment 7 android:id="@+id/fragment" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content"10 android:name="com.test.fragment.Myfragment"/>11 12 13 </LinearLayout>
fragment.xml
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 6 <TextView 7 android:id="@+id/textView1" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content"/>10 11 <Button 12 android:id="@+id/button1"13 android:layout_width="wrap_content"14 android:layout_height="wrap_content"15 android:text="改变"/>16 17 </LinearLayout>
2.java代码:
MainActivity.class
public class MainActivity extends Activity implements OnCheckedChangeListener { private RadioGroup rg1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rg1 = (RadioGroup) findViewById(R.id.radioGroup); rg1.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (checkedId) { case R.id.first:{ Intent it = new Intent(this,MainActivity2.class); startActivity(it); break; } case R.id.secnod:{ Myfragment2 fragment2 = new Myfragment2(); FragmentManager fragmentManager =getFragmentManager(); FragmentTransaction beginTransaction =fragmentManager.beginTransaction(); beginTransaction.add(R.id.linearLayout, fragment2); beginTransaction.addToBackStack(null); beginTransaction.commit(); break; } } } }
MainActivity2.class
1 package com.test.fragment; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.Button; 8 import android.widget.TextView; 9 10 public class MainActivity2 extends Activity {11 12 private TextView tv1;13 14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 // TODO Auto-generated method stub17 super.onCreate(savedInstanceState);18 setContentView(R.layout.main2);19 20 Button b1 = (Button) findViewById(R.id.button1);21 tv1 = (TextView) findViewById(R.id.textView1);22 b1.setText("change");23 b1.setOnClickListener(new OnClickListener() {24 25 @Override26 public void onClick(View v) {27 // TODO Auto-generated method stub28 tv1.setText("苦尽甘来");29 }30 });31 }32 33 }
Myfragment.class
1 package com.test.fragment; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView; 9 10 public class Myfragment extends Fragment{11 @Override12 public View onCreateView(LayoutInflater inflater, ViewGroup container,13 Bundle savedInstanceState) {14 // TODO Auto-generated method stub15 16 17 View view = inflater.inflate(R.layout.fragment, container, false); 18 19 TextView tv1 =(TextView) view. findViewById(R.id.textView1);20 tv1.setText("静态加载");21 return view;22 }23 24 }
Myfragment.class
1 package com.test.fragment; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.TextView; 9 10 public class Myfragment2 extends Fragment{11 @Override12 public View onCreateView(LayoutInflater inflater, ViewGroup container,13 Bundle savedInstanceState) {14 // TODO Auto-generated method stub15 16 17 View view = inflater.inflate(R.layout.fragment, container, false); 18 19 TextView tv1 =(TextView) view. findViewById(R.id.textView1);20 tv1.setText("动态加载");21 return view;22 }23 24 }
Android学习笔记之fragment的静态加载和动态加载
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。