今天我们讲下Fragment,其使用方法比较简单,但不是很好描述,我们直接看代码吧。
MainActivity:
package com.example.fragment_02;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
public class MainActivity extends Activity {
//Fragment管家
private FragmentManager manager;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//碎片管理器,用于管理Activity和Fragment之间的交互
manager = getFragmentManager();
//开启了Fragment的事务,方法:add()添加 replace()替换 remove()移除
FragmentTransaction transaction = manager.beginTransaction();
//将Fragment添加到指定容器当中
transaction.add(R.id.ll_left, new LeftFragment());
transaction.add(R.id.ll_right, new RightFragment());
//提交事务
transaction.commit();
}
}
左边的Fragment:
package com.example.fragment_02;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class LeftFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.left, null);
}
}
右边的Fragment:
package com.example.fragment_02;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class RightFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.right, null);
}
}
main_activity.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" >
<LinearLayout
android:id="@+id/ll_left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f00">
</LinearLayout>
<LinearLayout
android:id="@+id/ll_right"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#0ff">
</LinearLayout>
</LinearLayout>
左边的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"
android:background="#f00">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="左边"
android:textSize="20sp"/>
</LinearLayout>
右边的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"
android:background="#0ff">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="右边"
android:textSize="20sp"/>
</LinearLayout>
运行结果: