Android基础使用之Fragment详解一
来源:互联网 发布:毕业设计选题系统源码 编辑:程序博客网 时间:2024/04/28 20:12
相比较Fragment比Activity多了几个生命周期:
import android.app.Fragment;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class TestFragment extends Fragment { //当Fragment与Activity发生关联时调用。 @Override public void onAttach(Context context) { super.onAttach(context); } //创建该Fragment的视图 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return super.onCreateView(inflater, container, savedInstanceState); } //当Activity的onCreate方法返回时调用 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } //与onCreateView想对应,当该Fragment的视图被移除时调用 @Override public void onDestroyView() { super.onDestroyView(); } //与onAttach相对应,当Fragment与Activity关联被取消时调用 @Override public void onDetach() { super.onDetach(); }}
使用Fragment最简单的一种方式就是当普通控件用,直接写在Activity布局文件下。
activity_main.xml布局:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/fragment_title" android:name="com.mylistview.test.TestFragment" android:layout_width="match_parent" android:layout_height="wrap_content"></fragment></RelativeLayout>
fragment.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="wrap_content" android:orientation="horizontal" android:background="@color/colorAccent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="叮咚" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="芝麻开门" /></LinearLayout>
Fragment代码:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment, container, false); Button mButton = (Button) view.findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "欢迎来到我的博客", Toast.LENGTH_LONG).show(); } }); return view; }实现效果:
0 0
- Android基础使用之Fragment详解一
- 【Android基础】Fragment 详解之Fragment介绍
- 【Android基础】Fragment 详解之Fragment生命周期
- Android开发之Fragment详解(一)
- Android fragment详解(一 )
- Android之Fragment的使用(一)
- Android之Fragment的使用(一)
- Android中的Fragment使用详解之生命周期
- Android基础之Fragment
- Android Fragment使用详解
- Android Fragment使用详解
- Android Fragment使用详解
- Android Fragment使用详解
- Android Fragment 使用详解
- Android Fragment使用详解
- Android Fragment使用详解
- Android Fragment使用详解
- Android基础之Fragment与Activity交互详解
- 趣味程序设计_三色球问题
- 如何实施异构服务器的负载均衡及过载保护
- (三)网络请求
- insertion-sort-list
- Servlet Filter 简单示例
- Android基础使用之Fragment详解一
- web移动端Fixed在Input获取焦点时ios下产生的BUG及处理
- redis哨兵综述
- 控制台实现闪烁带颜色文字
- 红黑树 从入门到精通
- TestNG详解-深度好文
- ARM SoC漫谈
- Java EE 学习内容和顺序
- Java环境配置