Android学习——碎片Fragment的使用
一、碎片的简单用法(实现在一个活动中添加两个碎片,并让这两个碎片平分活动空间)
1、新建一个FragmentTest项目;
新建一个左侧碎片布局left_fragment.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">
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="Button"
- />
- </LinearLayout>
新建右侧碎片布局right_fragment.xml,代码如下:(布局背景设置成蓝色并放置一个TextView用于显示文本)
- <?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"
- android:background="@color/colorPrimaryDark">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:textSize="20sp"
- android:text="This is right fragment"
- />
- </LinearLayout>
2、新建一个LeftFragment类,并让它继承Fragment(注意这里的Fragment使用support-v4中的),代码如下:
- public class LeftFragment extends Fragment{
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
- View view=inflater.inflate(R.layout.left_fragment,container,false);
- return view;
- }
- }
重写了Fragment的onCreateView()方法,然后在这个方法中通过LayoutInflater的inflate()方法将刚才定义的left_fragment布局动态加载进来。
新建RightFragment类,同样继承Fragment,代码如下:
- public class RightFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
- View view=inflater.inflate(R.layout.right_fragment,container,false);
- return view;
- }
- }
3、修改activity_main.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"
- >
- <fragment
- android:id="@+id/left_fragment"
- android:name="com.example.administrator.fragmenttest.LeftFragment"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- />
- <FrameLayout
- android:id="@+id/right_layout"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- >
- </FrameLayout>
- </LinearLayout>
运行程序,效果如下:
二、动态添加碎片
1、在前面基础上新建another_right_fragment.xml,代码如下:(布局文件和right_fragment.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"
- android:background="#ffff00"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:textSize="20sp"
- android:text="This is another right fragment"
- />
- </LinearLayout>
2、新建AnotherRightFragment类作为另一个右侧碎片,代码如下:
- public class AnotherRightFragment extends Fragment{
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
- View view=inflater.inflate(R.layout.another_right_fragment,container,false);
- return view;
- }
- }
3、修改activity_main.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"
- >
- <fragment
- android:id="@+id/left_fragment"
- android:name="com.example.administrator.fragmenttest.LeftFragment"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- />
- <FrameLayout
- android:id="@+id/right_layout"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- >
- </FrameLayout>
- </LinearLayout>
4、修改MainActivity中的代码:(在代码中向FrameLayout里添加内容)
- public class MainActivity extends AppCompatActivity implements View.OnClickListener{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button button=(Button)findViewById(R.id.button);
- button.setOnClickListener(this);
- replaceFragment(new RightFragment());
- }
- @Override
- public void onClick(View v){
- switch(v.getId()){
- case R.id.button:
- replaceFragment(new AnotherRightFragment());
- break;
- default:
- break;
- }
- }
- private void replaceFragment(Fragment fragment){
- FragmentManager fragmentManager=getSupportFragmentManager();
- FragmentTransaction transaction=fragmentManager.beginTransaction();
- transaction.replace(R.id.right_layout,fragment);
- transaction.commit();
- }
- }
首先给左侧碎片中的按钮注册一个点击事件,然后调用replaceFrameLayout()方法动态添加RightFragment这个碎片,当点击左侧碎片中的按钮时,又会调用replaceFragment()方法将右侧碎片替换成AnotherRightFragment。
重新运行程序,可以看到和之前一样的界面,然后点击一下按钮,效果如下:
Android学习——碎片Fragment的使用的更多相关文章
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml
Android开发:碎片Fragment完全解析 为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...
- Android之碎片Fragment
Fragment是个特别的存在,有点像报纸上的专栏,看起来只占据页面的一小块,但是这一小块有自己的生命周期,可以自行其是,仿佛独立王国,并且这一小块的特性无论在哪个页面,给一个位置就行,添加以后不影响 ...
- android学习笔记Fragment的使用
Fragment的内容感觉好多啊,主要需要掌握Fragment静态加载,Fragment动态加载,Fragment的生命周期,Fragment与Activity的交互 1,Fragment的静态加载 ...
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml(转)
注明:这个转的,见谅未能标明原始出处 我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activ ...
- 【Android】碎片Fragment
1.碎片可以让界面在平板上更好地展示. 2.碎片是一种可以嵌入到活动中的UI片段,它能让程序更加合理和充分地利用一个大屏幕的空间.有自己的生命周期,能包含布局. 3.新建碎片类继承Fragment,可 ...
- Android交流会-碎片Fragment,闲聊单位与尺寸
女孩:又周末了哦~ 男孩:那么今日来开个交流会,我们也学一学人家高大尚的大会,自己开一个,广州站,Android开发攻城狮交流会~ 1.Fragment概要: Android从3.0开始引入了Frag ...
- android 开发 碎片Fragment布局例子(用按键切换碎片布局)
实现思路: 1.写一个父类布局,里面写一个按键和一个帧布局(用于给Fragment布局后续替代) 2.写3个子布局,并且在写3个class继承Fragment布局 3.在MainActivity的cl ...
- Android学习之Fragment解析
1.定义 Fragment中文解释是碎片的意思,主要用在大屏幕设备上,例如平板电脑上,支持更加动态和灵活的UI设计.Fragment在你的应用中相当于是一个模块化和可重用的组件,因为Fragm ...
- Android利用碎片fragment实现底部标题栏(Github模板开源)
在安卓开发当中,一个十分重要的布局则是底部标题栏了,拥有了底部标题栏,我们就拥有了整个软件UI开发的框架,一般而言,整个软件的布局首先就是从底部标题栏开始构建,然后再开始其他模块的编写,组成一个完善的 ...
随机推荐
- BZOJ [Poi2000]病毒 AC自动机_DFS_细节
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...
- appium的python异常处理
from appium import webdriver from selenium.common.exceptions import NoSuchElementException desired_ ...
- 【剑指Offer】32、把数组排成最小的数
题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. ...
- C++ 资源大全中文版
标准库 C++标准库,包括了STL容器,算法和函数等. C++ Standard Library:是一系列类和函数的集合,使用核心语言编写,也是C++ISO自身标准的一部分. Standard Tem ...
- [Ynoi2011]D2T1
题目大意: 给定一个数列$a$,有以下几种询问: 1. 给定$x$,在序列末尾插入$x$.2. 给定$l,r$,输出$\sum\limits_{i=l}^r a_i$.3. 给定$x$,将数列中的所有 ...
- MySQL高级 之 explain执行计划详解
使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的,分析你的查询语句或是表结构的性能瓶颈. explain执行计划包含的信息 其中最重要的字段为:i ...
- tp5 权限设置
============================== <?php/** * Created by PhpStorm. * User: 14155 * Date: 2018/11/10 * ...
- kotlin来了!!
小编前端时间由于工作比较繁忙,没有更新帖子... 最近又再技术博客上逛时发现了 “Kotlin”, 1.据听说挺厉害的,该语言是由战斗民族俄罗斯人发明的!! 2.据听说前后端都可以做 3.据听说安卓大 ...
- CentOS 6.3(x86_32)下安装Oracle 10g R2
一.硬件要求 1.内存 & swap Minimum: 1 GB of RAMRecommended: 2 GB of RAM or more 检查内存情况 # grep MemTotal / ...
- 【ACM】hdu_zs1_1001_水仙花数_201307271504
水仙花数 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submissio ...