Fragment对象的使用(一)
来源:互联网 发布:实惠猪软件 编辑:程序博客网 时间:2024/06/05 03:44
(1)新建名称为fragment的Android项目。布局文件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:id="@+id/layout1" android:orientation="vertical" android:gravity="center" > <Button android:id="@+id/btn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I am bttton"/></LinearLayout >
新建布局文件layout1.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" ><Button android:id="@+id/btn01" android:layout_marginTop="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I am " android:layout_centerHorizontal="true" android:layout_alignParentTop="true"/></LinearLayout>新建布局文件layout2.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" tools:context="${relativePackage}.${activityClass}" ><Button android:id="@+id/btn02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I" android:layout_centerHorizontal="true" android:layout_alignParentTop="true"/> </LinearLayout>(2)新建两个自定义的Fragment对象:
fragment1.java:
package com.example.fragment;import android.annotation.TargetApi;import android.app.Fragment;import android.os.Build;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;@TargetApi(Build.VERSION_CODES.HONEYCOMB)public class fragment1 extends Fragment{private View view;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.layout1, container, false);return view;}}
fragment2.java:
package com.example.fragment;import android.annotation.TargetApi;import android.app.Fragment;import android.os.Build;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;@TargetApi(Build.VERSION_CODES.HONEYCOMB)public class fragment2 extends Fragment{private View view;@Overridepublic View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle saveInstanceState){view=inflater.inflate(R.layout.layout2,container,false);return view;}}
这两个Fragment对象的代码仅仅是关联的布局的文件不同
(3)文件MainActivity.Java的核心代码如下:
package com.example.fragment;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Build;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.view.Menu;import android.widget.Button;@TargetApi(Build.VERSION_CODES.HONEYCOMB)@SuppressLint("NewApi")public class MainActivity extends FragmentActivity {private Button button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Inital();fragment1 fragment_1=new fragment1();fragment2 fragment_2=new fragment2();FragmentManager fm=this.getFragmentManager();FragmentTransactionft= fm.beginTransaction();ft.add(R.id.layout1,fragment_1);ft.add(R.id.layout1,fragment_2);ft.commit();}}
这样就实现了基本的fragment对象。
注:方法inflate()有3个参数,第一个参数表示Fragment对象关联的是哪个XML布局文件,第二个参数是把这个XML文件转成的View对象放入container容器中,第三个参数表示是否把这个View对象追加到container参数的后面。我们的第三个参数写得是false。如果这个参数是true,则代码应该是:
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {inflater.inflate(R.layout.layout1, container, true);return super.onCreateView(inflater, container, savedInstanceState);运行项目,也会出现正确的结果。
0 0
- Fragment对象的使用(一)
- Fragment的使用(一)
- Fragment使用(一)
- Fragment的进一步使用(一)
- Fragment的解析与使用(一)
- Fragment的使用介绍一(入门)
- Android--Fragment的使用(一)
- Fragment的使用小结(一)
- Android使用ViewPager+Fragment的使用(一)
- Fragment的概念与使用(一)--基本概念
- Fragment的概念与使用(一)--基本概念
- Android之Fragment的使用(一)
- Fragment的介绍和使用(一)
- Android之Fragment的使用(一)
- Fragment的数据交互 一 使用Handler
- Android Fragment 使用教程(一)
- Fragment(一)快速入门之使用
- Fragment总结(一)基本使用
- UIControl的两个子类 -- UISegmentedControl UISlider
- GDAL编译(2.x与1.x版本通用)
- BZOJ 2561: 最小生成树(最小割)
- PMI-ACP敏捷项目管理微课内容分享-敏捷宣言&角色(20151117 丁仿)
- IO测试工具 iometer 和pdtest的对比
- Fragment对象的使用(一)
- Android键盘“enter”键设置为“下一项”失效解决办法
- DIV CSS display (block none inline)属性的用法教程
- asn1学习笔记
- 宏函数
- SDWebImage清理缓存及其使用问题
- Android 图片滑动效果: ViewPager
- [TwistedFate]滑块UISlider 跑图UIImageView
- HDU 1290:献给杭电五十周年校庆的礼物【数学】