zoukankan      html  css  js  c++  java
  • Fragment简单用法

    一.示意图

     

    二.新建一个左侧碎片布局left_fragment.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="#ffff00">

        <Button

            android:text="加载内容"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:id="@+id/btn"

            android:layout_weight="1"

            android:onClick="myClick"/>

    </LinearLayout>

    三.创建左侧碎片类LeftFragment.java:

    注意:这里可能会有两个不同包下的Fragment 供你选择,建议使用android.app.Fragment,因为我们的程序是面向Android 4.0 以上系统的,另一个包下的Fragment 主要是用于兼容低版本的Android系统。

    public class LeftFragment extends Fragment {

               @Override

               public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                  View view = inflater.inflate(R.layout.left_fragment, container, false);

                  return view;

               }

    }

    四.新建一个右侧碎片布局right_fragment.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="#0000ff">

        <TextView

            android:text="TextView"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:id="@+id/textView2"

            android:layout_weight="1" />

    </LinearLayout>

    五.新建右侧碎片类RightFragment.java:

    public class RightFragment extends Fragment {

        @Override

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.right_fragment, container, false);

            return view;

        }

    }

    六.activity_main.xml布局

    <?xml version="1.0" encoding="utf-8"?>

    <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">

        <fragment

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:name="com.example.guo.fragment.LeftFragment"

            android:layout_weight="1"

            android:id="@+id/left_fragment" />

        <fragment

            android:layout_width="0dp"

            android:layout_height="match_parent"

            android:name="com.example.guo.fragment.RightFragment"

            android:layout_weight="1"

            android:id="@+id/right_fragment" />

    </LinearLayout>

    注意:对于fragment,在布局文件中,id是必须的,否则会报错

    七.新建平板模拟器,运行即可

  • 相关阅读:
    [转]开源游戏AI引擎列表与游戏中的人工智能
    [转]刨根问底!曲面细分技术到底是个啥?
    [转]link time code generation
    [转]PythonQt
    [转]Free Game Development Libraries
    Java虚拟机(二):垃圾回收算法 时间
    Java虚拟机(一):JVM的运行机制 时间
    Java虚拟机(四):常用JVM配置参数 时间
    Java虚拟机(三):垃圾收集器 时间
    mov offset和lea的区别
  • 原文地址:https://www.cnblogs.com/itfenqing/p/6732406.html
Copyright © 2011-2022 走看看