Adroid中Fragment的简单使用
2016-07-15 17:12
399 查看
下面四个步骤就能创建一个简单的fragment
1. 扩展Fragment class
2. 在XML 或 Java中提供显示
3. 覆盖onCreateView方法
4. 在activity中使用Fragment
如下就是简单的显例
创建一个FirstActivityFragment.java文件,扩展Fragment class
为Fragment创建一个xml fragment_first.xml
1. 扩展Fragment class
2. 在XML 或 Java中提供显示
3. 覆盖onCreateView方法
4. 在activity中使用Fragment
如下就是简单的显例
创建一个FirstActivityFragment.java文件,扩展Fragment class
package com.example.liang.login; import android.content.Context; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.VelocityTrackerCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; /** * Created by liang on 2016/7/15. */ public class FirstActivityFragment extends Fragment { public FirstActivityFragment(){ } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View v = inflater.inflate(R.layout.fragment_first, container, false); TextView textView = (TextView)v.findViewById(R.id.showFirstFragmentInfo); final Context context = this.getActivity(); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast hello; hello = Toast.makeText(context,"hello",Toast.LENGTH_LONG); hello.show(); } }); return v; } @Override public void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
为Fragment创建一个xml fragment_first.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/showFirstFragmentInfo" android:text="this is a firstFragment"/> </LinearLayout>并在一个activity view 中使用
<?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="vertical" android:background="#33FF00"> <fragment xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment" android:name="com.example.liang.login.FirstActivityFragment" tools:layout="@layout/fragment_first" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories