//布局文件的练习
leftFragment_layout.xml //此布局文件很简单就是一个button按钮
<?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="#ff0000"> <Button android:id="@+id/btn_run" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:text="this is fragment01"/> </LinearLayout>
//效果如下
/LeftFragment碎片
//RightFragment.xml // 此布局文件就一个Textviewpackage com.example.fragmentdemo; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2015/8/2. */ public class LeftFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //将leftFragment所对应的布局加载到fragment中 View view=inflater.inflate(R.layout.leftfragment_layout, container, false); 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:background="#00ff00"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="this is textview02"/> </LinearLayout>//效果如下
//RightFragment碎片
package com.example.fragmentdemo; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2015/8/2. */ public class RightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.rightfragment_layout,container,false); return view; } }
//第3个布局
<?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:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="28dp" android:text="another right fragment"/> </LinearLayout>//效果图
//实例代码
package com.example.fragmentdemo; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2015/8/2. */ public class AnotherRightFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加载布局文件 View view=inflater.inflate(R.layout.another_rightfragment_layout,container,false); return view; } }//Activity的布局
<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" tools:context=".MainActivity"> <fragment android:id="@+id/left_fgt" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:name="com.example.fragmentdemo.LeftFragment" /> <FrameLayout android:id="@+id/right_layout" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.example.fragmentdemo.RightFragment"/> </FrameLayout> </LinearLayout>//Activity示例代码
package com.example.fragmentdemo; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { //声明控件 private Button btn_run; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_run = (Button) this.findViewById(R.id.btn_run); btn_run.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_run: //实例化待添加的Fragment AnotherRightFragment anotherRightFragment=new AnotherRightFragment(); //实例化fragmentManager对象 FragmentManager fragmentManager=getFragmentManager(); //通过fragment开启事物 FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction(); //用添加的布局替换 replace 也就是动态的添加布局 fragmentTransaction.replace(R.id.right_layout,anotherRightFragment); //将事物添加到返回栈中 fragmentTransaction.addToBackStack(null); fragmentTransaction.commit();//提交 break; default: break; } } }//效果图如下: