Android学习笔记之Fragment的两种使用方法
一、第一种方法:
(1)Fragment的第一种使用方法是使用fragment加载单独的布局文件:(也就是xml的方式实现)
结构如下:
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="horizontal"
- tools:context=".MainActivity" >
- <LinearLayout
- android:id="@+id/linerlayout1"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:background="#CCCCCC"
- android:orientation="vertical" >
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="显示窗口" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/linerlayout2"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="3"
- android:background="#CCFFDD"
- android:orientation="vertical" >
- </LinearLayout>
- </LinearLayout>
right.xml是等会使用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:orientation="vertical" >
- <RatingBar
- android:id="@+id/ratingBar1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/button11"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="点我试试" />
- </LinearLayout>
MyFragment.java就是加载fragment的类,要继承Fragment类:(要重载父类的下边三个方法)
- package com.lc.tablet_fragment_addview;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.Toast;
- public class MyFragment extends Fragment {
- public MyFragment() {
- // TODO Auto-generated constructor stub
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // 这里的R.layout.right是界面的id
- View view = inflater.inflate(R.layout.right, null);
- Button button = (Button) view.findViewById(R.id.button11);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(getActivity(), "hello world!", Toast.LENGTH_LONG)
- .show();
- }
- });
- return view;
- }
- @Override
- public void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- }
- }
MainActivity.java:
- package com.lc.tablet_fragment_addview;
- import android.app.Activity;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button button;
- private FragmentManager fragmentManager; // 管理
- private FragmentTransaction fragmentTransaction; // 事务
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button) this.findViewById(R.id.button1);
- fragmentManager = getFragmentManager();
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- fragmentTransaction = fragmentManager.beginTransaction();
- MyFragment myFragment = new MyFragment();
- // 第一个参数是要放到哪个地方的id,第二个为要放入的fragment
- fragmentTransaction.add(R.id.linerlayout2, myFragment);
- fragmentTransaction.commit();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
演示效果:当点击灰色界面的按钮时显示右侧的布局:
二、第二种方法
- <RelativeLayout 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.example.tablet_fragment_fragementmanager.MyFragment"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="37dp" />
- </RelativeLayout>
- package com.example.tablet_fragment_fragementmanager;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- public class MyFragment extends Fragment {
- public MyFragment() {
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- /*
- * 这里只需找到布局文件即可
- */
- View view = inflater.inflate(R.layout.text, null);
- return view;
- }
- @Override
- public void onResume() {
- super.onResume();
- }
- }
- package com.example.tablet_fragment_fragementmanager;
- import android.app.Activity;
- import android.app.FragmentManager;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- /*
- * 再布局文件中拖入一个fragment、则使用下边的方法来找到特定的fragment
- * 不需要使用beginTransaction方法
- */
- public class MainActivity extends Activity {
- private MyFragment fragment;
- private FragmentManager fragmentManager;
- private Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- fragmentManager = getFragmentManager();
- // 使用fragmentManager找到fragment、使用ID作为唯一的标识符
- fragment = (MyFragment) fragmentManager
- .findFragmentById(R.id.fragment1);
- // 或者使用下边的方法找到fragment
- // fragment =(MyFragment)fragmentManager.findFragmentByTag("fragment1");
- // 找到fragment布局中的按钮button1
- button = (Button) fragment.getView().findViewById(R.id.button1);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(MainActivity.this, "hello world!",
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Android学习笔记之Fragment的两种使用方法相关推荐
- IOS学习之UISwitch控件两种使用方法和监听
IOS学习之UISwitch控件两种使用方法和监听 分类: IOS开发入门2012-06-15 11:48 1363人阅读 评论(0) 收藏 举报 一.第一种创建UISwitch控件的方法,在代码中动 ...
- 5G NR - RACH学习笔记3 - RACH的两种接入类型
CBRA(Contention Based - 基于竞争的随机接入) 基于竞争的RACH过程如下: 1) UE --> NW : MSG1(RACH Preamble, RA-RNTI-) 2) ...
- 开关电源环路学习笔记(4)-两种误差放大器的传递函数
前面几节我们已经说明白了,在满足一定条件的时候,Buck开关电源可以看成是线性系统,并且可以是划分为4级的反馈控制系统. 1.反馈级:H(s) 2.放大和补偿级:Gc(s) 3.PWM调制级:Gpwm ...
- Android学习笔记之——手机中几种自带传感器的应用
本博文学习一下Android中几种传感器的应用 目录 Android传感器 使用传感器步骤 Demo Test 查看手机支持哪些传感器 界面布局
- openvc学习笔记(4)——两种方法在没有环境下运行程序
参考博客:http://blog.csdn.net/xiaowei_cqu/article/details/8066329 OpenCV的安装总结 我们下载的OpenCV安装包一般包括源码,示例,文档 ...
- android学习笔记之Fragment(一)
2019独角兽企业重金招聘Python工程师标准>>> Fragment(Android3.0 API Level 11引入,之前的怎么办? 没关系可以使用Android suppo ...
- mybatis学习笔记(2)两种配置实现增删改查
第二天学习mybatis,发现框架的存在确实是省了我们很多时间,我们完全可以把一些操作简化,或者实现简单的封装提供我们使用.昨天是跟着教程云里雾里的实现了配置,今天再次使用就方便很多. String ...
- WebStorm学习笔记001---webstorm-删除项目 两种方法
webstorm上删除项目,竟然浪费了我十几分钟时间,这让我不得不很气愤的记录下来,以防像我一样的悲催男在webstorm js开发神器上删除项目都找不到. webstorm,首先关闭项目,然后移动到 ...
- 链表反转的两种实现方法,后一种击败了100%的用户!
作者 | 王磊 来源 | Java中文社群(ID:javacn666) 转载请联系授权(微信ID:GG_Stone) 链表反转是一道很基础但又非常热门的算法面试题,它也在<剑指Offer> ...
最新文章
- 身边的隐形富豪,都有哪些特征?
- 关于《红楼梦》的读后感优秀范文2000字
- mysql三范式_MySQL设计之三范式的理解
- python数字类型及运算_Python基础教程:运算符以及数据类型解析
- postgresql使用pg_rman备份恢复
- git上传代码前需要检查什么_肝功能检查前需要做什么准备?这6个要点需做好,以免准确度受影响...
- 计算机股票编程,计算机技术《股票软件编程》.doc
- 游戏开发需要懂几种语言?
- TCP/IP 报文格式(IP数据包、TCP报头、UDP报头)
- 梯度消失和梯度爆炸原因,表现,解决方案
- 简历中的工作经历要怎么写?
- 获取附近小区信息_小程序获取当前位置加搜索附近热门小区及商区的方法
- 电磁兼容(EMC):工程师必备之硬件EMC设计规范
- CRPR能补偿crosstalk吗?
- 解析 | 自动驾驶视觉定位与导航技术的研究与应用
- 青少年CTF训练平台Misc-Middle愿风神忽悠你题解
- n个点求 能构成多少个三角形
- MapperScannerConfigurer和MapperFactoryBean区别
- JS数据类型以及存储方式
- 关于奈奎斯特判据的通俗理解
热门文章
- 【Java 虚拟机原理】Dalvik 虚拟机 ( 打包 Jar 文件和 Dex 文件 | 反编译 Dex 文件 | 分析 Dex 文件反编译结果 )
- 【计算机网络】网络层 : 总结 ( 功能 | 数据交换 | IP 数据报 | IPv4 地址 | IPv6 地址 | 路由选择协议 | 路由算法 )★★★
- 【数据挖掘】贝叶斯公式应用 拼写纠正示例分析 ( 先验概率 | 似然概率 | 后验概率 )
- issubclass和isinstance 反射 内置方法(魔术方法)
- Windows 8各个版本的功能详解与对比【中文详表】
- DOM-添加元素、节点
- AngularJS 服务(Service)
- leetcode : Reverse Linked List II [two pointers]
- PC问题-该虚拟机似乎正在使用中
- (转)CentOS 5.5 64bit 编译安装Adobe FlashMediaServer 3.5