Fragment碎片是一种嵌入在Activity中的UI片段,他可以用来描述Activity中的一部分布局,如果Activity界面中的布局中的控件较多,比较复杂,我们可以使用Fragment把屏幕划分几个片段进行模块化管理。
一个Activity中可以包含多个Fragment,一个Fragment也可以在多个Activity中使用
Fragmen的创建有两种方法
一种通过写代码手动创建,另一种可以new Fragement
例:实现按钮点击切换Fragment
先把需要的图片复制进来:
创建一个包里面创建3个碎片Fragment
创建Fragment01:
package com.example.fragmenttest.FragementTest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.app.Fragment;
import com.example.fragmenttest.R;
public class Fragment01 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragement01,container,false);
return view;
}
}
创建fragment01.xml:修改背景图片:android:background="@drawable/c"> 还可以添加其他布局控件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/c">
</LinearLayout>
创建Fragment02:直接复制上面创建好的Fragment01,更改名字为Fragment02
package com.example.fragmenttest.FragementTest;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.fragmenttest.R;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragment02 extends Fragment {
public Fragment02() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragement02, container, false);
}
}
创建fragment02.xml:可以复制布局fragment01.xml,修改名字fragment02.xml:修改背景颜色:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@drawable/a"
tools:context=".FragementTest.Fragment02">
</FrameLayout>
创建Fragment03:直接复制上面创建好的Fragment01,更改名字为Fragment03
package com.example.fragmenttest.FragementTest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
import com.example.fragmenttest.R;
/**
* A simple {@link Fragment} subclass.
*/
public class Fragment03 extends Fragment {
public Fragment03() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragement03, container, false);
}
}
创建fragment03.xml:可以复制布局fragment01.xml,修改名字fragment03.xml:修改背景颜色:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@drawable/m"
tools:context=".FragementTest.Fragment02">
</FrameLayout>
创建MainActivity:
package com.example.fragmenttest;
import androidx.appcompat.app.AppCompatActivity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import com.example.fragmenttest.FragementTest.Fragment01;
import com.example.fragmenttest.FragementTest.Fragment02;
import com.example.fragmenttest.FragementTest.Fragment03;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn01;
private Button btn02;
private Button btn03;
private Button btn04;
private FrameLayout fl_show ;
private Fragment01 fragment01;
private Fragment02 fragment02;
private Fragment03 fragment03;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
initSetOnClick();
}
//下一个点击按钮的方法
private void initSetOnClick() {
btn01.setOnClickListener(this);//监听器
btn02.setOnClickListener(this);//由于该类继承了view的监听,因此设置监听的参数只需传本类的对象即可
btn03.setOnClickListener(this);
btn04.setOnClickListener(this);
}
//首先获取所有控件的id,写在一个方法里面
private void init() {
btn01 = (Button)findViewById(R.id.btn01);//找到id
btn02 = (Button)findViewById(R.id.btn02);
btn03 = (Button)findViewById(R.id.btn03);
btn04 = (Button)findViewById(R.id.btn04);
fl_show = (FrameLayout)findViewById(R.id.fl_show);
fragment01 = new Fragment01();//创建
fragment02 = new Fragment02();
fragment03 = new Fragment03();
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn01:
Intent intent= new Intent(this,MainActivity.class);
startActivity(intent);
break;
case R.id.btn02:
FragmentManager FM=getFragmentManager();//获得FragmentManager实例
FragmentTransaction fT=FM.beginTransaction();//获取 FragmentTransaction实例
fT.replace(R.id.fl_show,fragment01);//添加一个Fragment
fT.commit();//提交事务
break;
case R.id.btn03:
FragmentManager FM1=getFragmentManager();
FragmentTransaction fT1=FM1.beginTransaction();
fT1.replace(R.id.fl_show,fragment02);
fT1.commit();
break;
case R.id.btn04:
FragmentManager FM2=getFragmentManager();
FragmentTransaction fT2=FM2.beginTransaction();
fT2.replace(R.id.fl_show,fragment03);
fT2.commit();
break;
}
}
}
创建activity_main.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fl_show"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/btn01"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="主界面">
</Button>
<Button
android:id="@+id/btn02"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="风景1">
</Button>
<Button
android:id="@+id/btn03"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="风景2">
</Button>
<Button
android:id="@+id/btn04"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="风景3">
</Button>
</LinearLayout>
</LinearLayout>
结果:
点击风景1:
点击风景2:
点击主页面: