Fragment之一:Fragment入门

mac2022-06-30  23

参考自张泽华视频

Fragment是自Android3.0后引入的特性,主要用于在不同的屏幕尺寸中展现不同的内容。

Fragment必须被嵌入Activity中使用,总是作为Activity的组成部分。

简单示例:

一个Activity的界面由2个部分组成,每个部分分别是一个Fragment。

效果图如下:

1、创建第一个Fragment的界面文件。

Fragment的界面文件和一般的Activity布局文件一样,如。

<?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="#0000ff"     android:orientation="vertical" >          <TextView          android:layout_width="match_parent"         android:layout_height="match_parent"         android:text="@string/fragment1"         /> </LinearLayout>

2、创建第一个Fragment的类文件。

package com.ljh.fragmentdemo; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; //继续Fragment类。 public class Fragment1 extends Fragment { @Override //在Fragment被加载时调用 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加载某个布局文件。 return inflater.inflate(R.layout.fragment1, null); } }

3、创建第二个Fragment的界面文件。

<?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="match_parent" android:layout_height="match_parent" android:text="@string/fragment2"/> </LinearLayout>

4、创建第二个Fragment的类文件。

package com.ljh.fragmentdemo; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; //继续Fragment类。 public class Fragment2 extends Fragment { @Override //在Fragment被加载时调用 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加载某个布局文件。 return inflater.inflate(R.layout.fragment2, null); } } 5、创建主界面文件。

<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: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=".MainActivity" >     <fragment         android:id="@+id/fragment1"         android:name="com.ljh.fragmentdemo.Fragment1"         android:layout_width="0dp"         android:layout_height="match_parent"         android:layout_weight="1" />     <fragment         android:id="@+id/fragment2"         android:name="com.ljh.fragmentdemo.Fragment2"         android:layout_width="0dp"         android:layout_height="match_parent"         android:layout_weight="1" /> </LinearLayout>

6、创建主类。

本步骤的关键是用Fragment对象取代布局文件中的内容。

package com.ljh.fragmentdemo; import com.ljh.fragmentdemo.R; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获得FragmentManager,然后获取FragmentTransaction。 FragmentManager fm = getFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); //用Fragment动态代替布局文件中的内容 transaction.replace(R.id.fragment1, new Fragment1()); transaction.replace(R.id.fragment2, new Fragment2()); //提交事务 transaction.commit(); } }

注:

1、对两个Fragment的分别进行编辑,如

@Override //在Fragment被加载时调用 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //加载某个布局文件。 View root = inflater.inflate(R.layout.fragment1, null); TextView tvContent = (TextView) root.findViewById(R.id.tv_content); tvContent.setText("hello"); return root; } 2、其它内容见后面博客:

(1)Android2.3及以前对Fragment的支持。

(2)使用Fragment使不同的尺寸的屏幕展现不同内容。

(3)Activity与Fragment之间的通信。

转载于:https://www.cnblogs.com/jinhong-lu/p/4559494.html

相关资源:Android app开发中的Fragment入门学习教程
最新回复(0)