[Android新手学习笔记26]-Fragment简单例子

来源:互联网 发布:网络直播软件下载 编辑:程序博客网 时间:2024/06/18 09:00

简单例子:

创建layout/left_fragment.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent">
  7.    <Button
  8.        android:id="@+id/button"
  9.        android:layout_gravity="center_horizontal"
  10.        android:text="按钮"
  11.        android:layout_width="wrap_content"
  12.        android:layout_height="wrap_content" />
  13. </LinearLayout>

创建layout/right_fragment.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:background="#00ff00"
  6.    android:layout_width="match_parent"
  7.    android:layout_height="match_parent">
  8.    <TextView
  9.        android:id="@+id/text_view"
  10.        android:layout_gravity="center_horizontal"
  11.        android:textSize="20sp"
  12.        android:text="这是右侧部分"
  13.        android:layout_width="wrap_content"
  14.        android:layout_height="wrap_content" />
  15. </LinearLayout>

修改activity_main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="horizontal"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent">
  7.    <fragment
  8.        android:id="@+id/left_fragment"
  9.        android:name="top.xiexiaodong.fragmentdemo.LeftFramgment"
  10.        android:layout_width="0dp"
  11.        android:layout_weight="1"
  12.        android:layout_height="match_parent" />
  13.    <fragment
  14.        android:id="@+id/right_fragment"
  15.        android:name="top.xiexiaodong.fragmentdemo.RightFragment"
  16.        android:layout_width="0dp"
  17.        android:layout_weight="1"
  18.        android:layout_height="match_parent" />
  19. </LinearLayout>

创建LeftFragment类,继承android.support.v4.app.Fragment:

  1. public class LeftFramgment extends Fragment {
  2.    @Nullable
  3.    @Override
  4.    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  5.        View view = inflater.inflate(R.layout.left_fragment, container, false);
  6.        return view;
  7.    }
  8. }

创建RightFragment类,继承android.support.v4.app.Fragment:

  1. public class RightFragment extends Fragment {
  2.    @Nullable
  3.    @Override
  4.    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  5.        View view = inflater.inflate(R.layout.right_fragment, container, false);
  6.        return view;
  7.    }
  8. }


动态添加碎片:

创建anther_right_fragment.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="vertical"
  5.    android:background="#ffff00"
  6.    android:layout_width="match_parent"
  7.    android:layout_height="match_parent">
  8.    <TextView
  9.        android:id="@+id/text_view"
  10.        android:layout_gravity="center_horizontal"
  11.        android:textSize="20sp"
  12.        android:text="这是另一个右侧部分"
  13.        android:layout_width="wrap_content"
  14.        android:layout_height="wrap_content" />
  15. </LinearLayout>


修改activity_main:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="horizontal"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent">
  7.    <fragment
  8.        android:id="@+id/left_fragment"
  9.        android:name="top.xiexiaodong.fragmentdemo.LeftFramgment"
  10.        android:layout_width="0dp"
  11.        android:layout_weight="1"
  12.        android:layout_height="match_parent" />
  13.    <FrameLayout
  14.        android:id="@+id/right_fragment"
  15.        android:layout_width="0dp"
  16.        android:layout_weight="1"
  17.        android:layout_height="match_parent" >
  18.    </FrameLayout>
  19. </LinearLayout>


创建AntherFragment类:

  1. public class AntherRightFragment extends Fragment {
  2.    @Nullable
  3.    @Override
  4.    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  5.        View view = inflater.inflate(R.layout.anther_right_fragment, container, false);
  6.        return view;
  7.    }
  8. }


修改MainActivity:

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  2.    @Override
  3.    protected void onCreate(Bundle savedInstanceState) {
  4.        super.onCreate(savedInstanceState);
  5.        setContentView(R.layout.activity_main);
  6.        Button button = (Button) findViewById(R.id.button);
  7.        button.setOnClickListener(this);
  8.    }
  9.    @Override
  10.    public void onClick(View v) {
  11.        switch (v.getId()) {
  12.            case R.id.button:
  13.                replaceFragment(new AntherRightFragment());
  14.                break;
  15.            default:
  16.        }
  17.    }
  18.    private void replaceFragment(Fragment fragment) {
  19.        FragmentManager fragmentManager = getSupportFragmentManager();
  20.        FragmentTransaction transaction = fragmentManager.beginTransaction();
  21.        transaction.replace(R.id.right_fragment, fragment);
  22.        transaction.commit();
  23.    }
  24. }


添加到返回栈,效果,按Back键,返回上一个Fragment:

  1. private void replaceFragment(Fragment fragment) {
  2.    FragmentManager fragmentManager = getSupportFragmentManager();
  3.    FragmentTransaction transaction = fragmentManager.beginTransaction();
  4.    transaction.replace(R.id.right_fragment, fragment);
  5.    transaction.addToBackStack(null);
  6.    transaction.commit();
  7. }


传入null即可。

碎片和活动之间进行通信:

活动获取碎片:

  1. RightFragment rightFragment = (RightFragment)  getSupportFragmentManager().findViewById(R.id.right_fragment);


碎片获取活动:

  1. MainActivity mainActivity = (MainActivity) getActivity();


之后就可以使用碎片或者活动方法了。

使用限定符:

修改activity_main:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="horizontal"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent">
  7.    <fragment
  8.        android:id="@+id/left_fragment"
  9.        android:name="top.xiexiaodong.fragmentdemo.LeftFramgment"
  10.        android:layout_width="match_parent"
  11.        android:layout_height="match_parent" />
  12. </LinearLayout>


右键res文件夹,创建Android Resource File,File name:activity_main,和上面一致,Resource type:layout,Available qualifiers:Size,Chosen qualifiers(点击中间>>按钮):large,点击OK:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    android:orientation="horizontal"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent">
  7.    <fragment
  8.        android:id="@+id/left_fragment"
  9.        android:name="top.xiexiaodong.fragmentdemo.LeftFramgment"
  10.        android:layout_width="0dp"
  11.        android:layout_weight="1"
  12.        android:layout_height="match_parent" />
  13.    <fragment
  14.        android:id="@+id/right_fragment"
  15.        android:name="top.xiexiaodong.fragmentdemo.RightFragment"
  16.        android:layout_width="0dp"
  17.        android:layout_weight="3"
  18.        android:layout_height="match_parent" />
  19. </LinearLayout>


修改MainActivity:

  1. public class MainActivity extends AppCompatActivity {
  2.    @Override
  3.    protected void onCreate(Bundle savedInstanceState) {
  4.        super.onCreate(savedInstanceState);
  5.        setContentView(R.layout.activity_main);
  6.    }
  7. }


运行,这样,就可以在large设备显示两列,在其他设备显示一列。其他限定符如下:

                                                       

屏幕特征限定符描述大小small提供给小屏幕设备的资源normal提供给中等屏幕设备的资源large提供给大屏幕设备的资源xlarge提供给超大屏幕设备的资源分辨率ldpi提供给低分辨率设备的资源(120dpi以下)mdpi提供给中等分辨率设备的资源(120dpi~160dpi)hdpi提供给高分辨率设备的资源(160dpi~240dpi)xhdpi提供给超高分辨率设备的资源(240dpi~320dpi)xxhdpi提供给超超高分辨率设备的资源(320dpi~480dpi)方向land提供给横屏设备的资源port提供给竖屏设备的资源


最小限定符:

在res目录下新建layout-sw600dp文件夹,只要大于600dpi都都使用。

0 0
原创粉丝点击