[android] 01_fragment入门-静态的fragment(在activity_main.xml中写死了fragment)

Android 4.0

fragment

fragment
1、手机屏幕越来越大,有的内容一个小手机屏幕就能显示完全,而对于一个大的手机屏幕,就浪费了,
fragment,帧,片段,是Activity的一部分
2、布局文件中,开始标签大写开头表示是一个View对象,小写表示是一个类型像fragment
3、布局文件中用name属性,将要显示的Fragment类指定,该类中要复写onCreateView(),指定显示的View,否则会报错
步骤:
1、定义两个布局显示文件
2、在main.xml布局文件中,声明fragment类型,并制定绑定的实现类
3、定义fragment的实现类,重写里面的onCreateView()方法,并inflate前面定义的布局view
1、布局文件
a) /res/layout/fragment1.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:gravity="center"
    android:background="#00ff00"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment1"/>
</LinearLayout>
b) /res/layout/fragment2.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:gravity="center"
    android:background="@android:color/black"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment2"
        android:textColor="#ff0000"
        />
</LinearLayout>
c) /res/layout/activity_main.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="horizontal" >
    <fragment android:name="cn.zengfansheng.fragment.Fragment1"
        android:id="@+id/fragment1"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_width="0dp"
        />
    <fragment android:name="cn.zengfansheng.fragment.Fragment2"
        android:id="@+id/fragment2"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_width="0dp"
        />
</LinearLayout>
2、核心代码:
a) Fragment1.java
package cn.zengfansheng.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1null);
    }
}
b) Fragment2.java
package cn.zengfansheng.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment2null);
    }
}
c) MainActivity.java
package cn.zengfansheng.fragment;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
3、结果:
问题:没有return对
public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1null);
    }
}