今天说讲讲静态fragment的使用
<fragment
android:name="com.example.fragment1.FirstFragment"
android:layout_weight="1"
android:id="@+id/first_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
其他属性不做说明了,就说一下name属性,那么属性指向的是一个全类名,必须是一个继承fragment或者继承fragment的子类
这里我们使用的import android.support.v4.app.Fragment下的fragment,MainActivity必须继承FragmentActivity,才可以使用
fragment,当然你也可以不适用v4下的fragment,直接使用import android.app.FragmentManager;但是必须同意一种类型,其他的必须继承fragment,重写onCreateView方法,放回当前的view,记住fragment这种情况就是当作一种控件
MainActivity.java
package com.example.fragment1;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.fragment1.MainActivity" >
<!-- name决定加载那个fragment -->
<fragment
android:name="com.example.fragment1.FirstFragment"
android:layout_weight="1"
android:id="@+id/first_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<fragment
android:name="com.example.fragment1.SecondFragment"
android:layout_weight="1"
android:id="@+id/second_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
firstfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#ff0000"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_centerInParent="true"
android:text="firstfragment"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>
FirstFragment.java
package com.example.fragment1;
import com.example.fragment1.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.firstfragment, container, false);
return view;
}
}
secondfragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#454545"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_centerInParent="true"
android:text="secondfragment"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</RelativeLayout>
SecondFragment.java
package com.example.fragment1;
import com.example.fragment1.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment{
//初始化fragment视图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.secondfragment, container, false);
return view;
}
}
结果图:
这是一个简单的静态fragment示例,这种方法不灵活,是写死了的,所以在下一篇博客中我会写一个动态的,数据可以手动动态切换的示例