组件--Fragment(碎片)第二篇详解
感觉之前看的还是不清楚,重新再研究了一次
Fragment常用的三个类:
android.app.Fragment 主要用于定义Fragment
android.app.FragmentManager 主要用于在Activity中操作Fragment
android.app.FragmentTransaction 保证一些列Fragment操作的原子性,熟悉事务这个词,一定能明白~
1. 静态调用(就充当普通的控件)
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="viewpagerindicator.com.fragmentstatic"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </application> </manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!--静态调用fragment的方法:
就是相当于把fragment当做一个Button或者EditText之类的空间
其中name就相当于普通空间的ID-->
<fragment
android:layout_width="match_parent"
android:layout_height="45dp"
android:id="@+id/fragment1"
android:name="viewpagerindicator.com.fragmentstatic.FragmentTittle"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="区隔按键"/> <fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_context_id"
android:name="viewpagerindicator.com.fragmentstatic.FragmentContext"/> </LinearLayout>

fragment_tittle.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="45dp"
android:orientation="horizontal"> <Button
android:text="按键"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:textSize="15sp"
/> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试FRAGMENT"
android:gravity="center"
/>
</RelativeLayout>

fragment_content.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="match_parent"
android:orientation="horizontal"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:text="显示区域"
android:textSize="40dp"
android:gravity="center"/>
</LinearLayout>

MainActivity.java
package viewpagerindicator.com.fragmentstatic; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; public class MainActivity extends AppCompatActivity {
final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
}
FragmentTittle.java
package viewpagerindicator.com.fragmentstatic; import android.app.Fragment;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast; public class FragmentTittle extends Fragment { Button mLeftMenu;
final String TAG = "FragmentTittle";
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_tittle, container, false);
mLeftMenu = (Button) view.findViewById(R.id.bt1);
mLeftMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "按键点击事件", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
FragmentContext.java
package viewpagerindicator.com.fragmentstatic; import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class FragmentContext extends Fragment { final String TAG = "FragmentContext";
//Button mLeftMenu; public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_content, container, false);
//mLeftMenu = (Button) view.findViewById(R.id.bt1);
return view;
}
}


2. 动态调用
组件--Fragment(碎片)第二篇详解的更多相关文章
- Scala进阶之路-Scala函数篇详解
Scala进阶之路-Scala函数篇详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.传值调用和传名调用 /* @author :yinzhengjie Blog:http: ...
- 转载 JS组件Bootstrap Select2使用方法详解
JS组件Bootstrap Select2使用方法详解 作者:懒得安分 字体:[增加 减小] 类型:转载 时间:2016-01-26我要评论 这篇文章主要为大家介绍了JS组件Bootstrap Sel ...
- PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明
PHP函数篇详解十进制.二进制.八进制和十六进制转换函数说明 作者: 字体:[增加 减小] 类型:转载 中文字符编码研究系列第一期,PHP函数篇详解十进制.二进制.八进制和十六进制互相转换函数说明 ...
- 走向DBA[MSSQL篇] 详解游标
原文:走向DBA[MSSQL篇] 详解游标 前篇回顾:上一篇虫子介绍了一些不常用的数据过滤方式,本篇详细介绍下游标. 概念 简单点说游标的作用就是存储一个结果集,并根据语法将这个结果集的数据逐条处理. ...
- Android UI组件----AppWidget控件入门详解
Widget引入 我们可以把Widget理解成放置在桌面上的小组件(挂件),有了Widget,我们可以很方便地直接在桌面上进行各种操作,例如播放音乐. 当我们长按桌面时,可以看到Widget选项,如下 ...
- Android组件---四大布局的属性详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...
- QuantLib 金融计算——基本组件之天数计算规则详解
目录 天数计算规则详解 定义 30 / 360 法 30/360 US 30/360 Bond Basis 30E/360 30E/360 ISDA Actual 法 Actual/Actual IC ...
- Spring--开篇 (spring优缺点、模块组件、各个jar包详解)
Spring--开篇 分类: SSH&EJB2012-11-23 15:25 4369人阅读 评论(13) 收藏 举报 javaJavaJAVAspringSpringwebWebWEB框架 ...
- Android UI组件----ListView列表控件详解
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...
随机推荐
- Flex 布局里 input 宽度最小 150px 的问题, 浏览器 BUG?
今天在使用 flex 布局时, 发现当 flex 布局容器比小(小于 150px )时,里面的 input[text] 的宽度会比容器宽: <style> #main { width:12 ...
- TestNG参数化之@Parameters传参
通过TestNG实现参数话常用两种方式,一种是借助 @Parameters读取testng.xml中参数,一种是使用@DataProvider注解传参. 此次主要讲解XML传参,语法:在java类中定 ...
- Android 如何查看源码 (eclipse 按住 ctrl )
首先要确认 Android SDK Manager 下载并安装了 sources.然后在代码中按住 ctrl 在点一个类名, 如果打开的页面是找不到源码,就点那个按钮,然后找到源码所在的文件夹,就可以 ...
- Android 的一些中文文档
https://blog.csdn.net/qq_36467463/article/details/77990089 //安卓mediaformat api详解 https://www.cnbl ...
- Mybatis 优化:
Mybatis 的优化: ** 第一个 对于数据库配置的优化: 创建一个 DB.properties 的文件 里面编写Key = value 形式的数据库信息 比如: driver = com.mys ...
- loadrunner录制的基本知识
1.http/html录制选择web_url,如下图所示: 开始录制->Options->Recording->HTML Advanced->选择web_url->OK ...
- elasticsearch5.2.1使用logstash同步mysql
centos 本人亲测可以 首先安装好mysql,elasticsearch 不懂的请参考另一篇文章 安装logstash官方:https://www.elastic.co/guide/en/l ...
- XCode7无证书真机调试教程
转载自http://altair21.com/156.html 前提条件: XCode版本>=7 1. 进入xcode,菜单栏选择xcode –> preferences (快捷键 com ...
- php 内存分配新
https://yq.aliyun.com/articles/38307 https://yq.aliyun.com/ziliao/132720 http://blog.liyiwei.cn/%E3% ...
- Linux之virtualbox中的ubuntu虚拟机linux系统共享文件夹
windows通过virtualbox软件与linux系统机型文件共享 1.第一步 在设置中找到共享文件夹选项,选择添加共享文件夹 2.第二步 选择需要与linux进行共享的文件夹,并选择固定分配 3 ...