您的位置:首页 > 移动开发 > Android开发

Android----Fragment的基本用法

2015-10-22 10:10 375 查看
Android是在Android 3.0 (API level 11)开始引入Fragment的。
  可以把Fragment想成Activity中的模块,这个模块有自己的布局,有自己的生命周期,单独处理自己的输入,在Activity运行的时候可以加载或者移除Fragment模块。

  可以把Fragment设计成可以在多个Activity中复用的模块。

fragment的实例:

      实现功能:根据横竖屏显示不同的内容。

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:background="#aa0000ff"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="35sp"
android:text="横屏显示"/>

</LinearLayout>

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:background="#00ff00"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="35sp"
android:text="竖屏显示"/>

</LinearLayout>要显示fragment中的内容需要继承类Fragment,分别创建类Fragment1、Fragment2。
public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
return view;
}
}
public class Fragment2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, null);
}
}


MainActivity.java
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int width = getWindowManager().getDefaultDisplay().getWidth();
int length = getWindowManager().getDefaultDisplay().getHeight();

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

if (width < length) {
ft.replace(android.R.id.content, new Fragment2());
} else {
ft.replace(android.R.id.content, new Fragment1());
}
ft.commit();
}

}

需要注意的是:Fragment的使用需要开启事务,最后要提交。
<span style="color:#FF0000;">FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();</span>

<span style="color:#FF0000;">ft.commit();</span>

实现的效果:



另一个fragment的例子,实现的功能:分模块显示内容

创建4个模块要显示的内容:

<?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:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体育新闻" />

</LinearLayout>
<?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:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="社会新闻" />

</LinearLayout>

<?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:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="国内新闻" />

</LinearLayout>

<?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:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="国际新闻" />

</LinearLayout>


布局文件
为了让点击时更容易识别到点击的效果,在drawable下添加以下文件font_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:color="#00aaff"/>
<item android:state_pressed="true" android:color="#33aaff"/>
<item android:color="#1177ff"/>

</selector>
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"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >

<TextView
android:id="@+id/tab1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#aa1265"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="体育新闻" />

<TextView
android:id="@+id/tab2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#32a221"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="社会新闻" />

<TextView
android:id="@+id/tab3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#387221"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="国内新闻" />

<TextView
android:id="@+id/tab4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#321cd1"
android:gravity="center"
android:textColor="@drawable/font_color"
android:text="国际新闻" />
</LinearLayout>

<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>

</LinearLayout>

实现效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  fragment