问:在Android xml布局中Fragment与FrameLayout的区别?

答:在Activity中托管一个UI Fragment有两种方式:

  1. 1)添加fragmentactivity布局中。用name属性表明需要显示的fragment<br /> 2)在activity代码中动态添加删除fragmentFrameLayout并不是相对布局,它就是一个容器,向其中添加的任何子组件,需要通过layout_gravity属性值决定在父视图中的位置,而各个子组件并不能像RelativeLayout一样,定义相对位置。

一、fragment的产生

  1. 平板电脑界面动态灵活设计<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1622970728757-f2589f91-4e1d-4b89-a6e1-04a789c076a7.png#clientId=udc139a95-92f9-4&from=paste&height=376&id=u0fdaa85d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=376&originWidth=588&originalType=binary&ratio=1&size=18057&status=done&style=none&taskId=u839b856d-c93c-4179-862d-d5b3d279a1f&width=588)<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1622971163020-afbf0e9a-45a9-4620-8beb-133bedd609d4.png#clientId=udc139a95-92f9-4&from=paste&height=375&id=u14646978&margin=%5Bobject%20Object%5D&name=image.png&originHeight=375&originWidth=560&originalType=binary&ratio=1&size=19472&status=done&style=none&taskId=u0e51b063-d8f7-414b-b1c4-6b0a00fdbf6&width=560)

二、什么是fragment

1.具备生命周期,子activity

2.比须委托在activity中才能运行

三、压缩包

fragmentbase.zip

四、实现具体步骤

1.创建fragment

image.png

2.在碎片的xml文件中设置布局

image.png

3.在片段逻辑代码中,对布局进行逻辑处理

image.png

4.在主布局xml文件中进行片段绑定activity数组

image.png

五、整体代码

1.布局activity_main.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity"
  8. android:orientation="vertical">
  9. <fragment
  10. android:id="@+id/fr_item"
  11. android:name="com.example.fragmentbase.fragment.BlankFragment1"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. />
  15. </LinearLayout>

2.布局fragment_blank_fragment1.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. tools:context=".fragment.BlankFragment1">
  8. <!-- TODO: Update blank fragment layout -->
  9. <TextView
  10. android:id="@+id/tv_item"
  11. android:layout_width="match_parent"
  12. android:layout_height="40dp"
  13. android:text="@string/hello_blank_fragment" />
  14. <Button
  15. android:layout_width="match_parent"
  16. android:layout_height="40dp"
  17. android:text="how are you"
  18. android:id="@+id/btn_item"
  19. />
  20. </LinearLayout>

3.MainActivity文件代码

  1. package com.example.fragmentbase;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. public class MainActivity extends AppCompatActivity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. }
  10. }

4.BlankFragment1文件代码

  1. package com.example.fragmentbase.fragment;
  2. import android.os.Bundle;
  3. import androidx.fragment.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. import com.example.fragmentbase.R;
  10. public class BlankFragment1 extends Fragment {
  11. private View rootView;
  12. private TextView textView;
  13. private Button button;
  14. @Override
  15. public void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. }
  18. @Override
  19. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  20. Bundle savedInstanceState) {
  21. if (rootView==null) {
  22. rootView = inflater.inflate(R.layout.fragment_blank_fragment1, container, false);
  23. }
  24. textView=rootView.findViewById(R.id.tv_item);
  25. button=rootView.findViewById(R.id.btn_item);
  26. button.setOnClickListener(new View.OnClickListener() {
  27. @Override
  28. public void onClick(View v) {
  29. textView.setText("Yes,I am,and you?");
  30. }
  31. });
  32. return rootView;
  33. }
  34. }

5.效果图:

  1. 1)运行之后<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1623332100142-9c264b82-1018-45af-b8de-b259753c890d.png#clientId=uef992d2e-de25-4&from=paste&height=573&id=uac2732b1&margin=%5Bobject%20Object%5D&name=image.png&originHeight=573&originWidth=885&originalType=binary&ratio=1&size=90831&status=done&style=none&taskId=uad09dfd8-220c-4066-af02-27483bafe03&width=885)<br /> (2)点击按钮之后<br />![image.png](https://cdn.nlark.com/yuque/0/2021/png/1574587/1623332127882-ab0c51a2-52b5-45ba-a377-97c48e9a192a.png#clientId=uef992d2e-de25-4&from=paste&height=602&id=ub48756fd&margin=%5Bobject%20Object%5D&name=image.png&originHeight=602&originWidth=886&originalType=binary&ratio=1&size=94124&status=done&style=none&taskId=uf3f2b95b-8538-46ae-952c-c2d0e8cf0ff&width=886)