一、碎片的简单用法(实现在一个活动中添加两个碎片,并让这两个碎片平分活动空间)

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的使用的更多相关文章

  1. Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml

    Android开发:碎片Fragment完全解析   为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...

  2. Android之碎片Fragment

    Fragment是个特别的存在,有点像报纸上的专栏,看起来只占据页面的一小块,但是这一小块有自己的生命周期,可以自行其是,仿佛独立王国,并且这一小块的特性无论在哪个页面,给一个位置就行,添加以后不影响 ...

  3. android学习笔记Fragment的使用

    Fragment的内容感觉好多啊,主要需要掌握Fragment静态加载,Fragment动态加载,Fragment的生命周期,Fragment与Activity的交互 1,Fragment的静态加载 ...

  4. Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml(转)

    注明:这个转的,见谅未能标明原始出处 我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activ ...

  5. 【Android】碎片Fragment

    1.碎片可以让界面在平板上更好地展示. 2.碎片是一种可以嵌入到活动中的UI片段,它能让程序更加合理和充分地利用一个大屏幕的空间.有自己的生命周期,能包含布局. 3.新建碎片类继承Fragment,可 ...

  6. Android交流会-碎片Fragment,闲聊单位与尺寸

    女孩:又周末了哦~ 男孩:那么今日来开个交流会,我们也学一学人家高大尚的大会,自己开一个,广州站,Android开发攻城狮交流会~ 1.Fragment概要: Android从3.0开始引入了Frag ...

  7. android 开发 碎片Fragment布局例子(用按键切换碎片布局)

    实现思路: 1.写一个父类布局,里面写一个按键和一个帧布局(用于给Fragment布局后续替代) 2.写3个子布局,并且在写3个class继承Fragment布局 3.在MainActivity的cl ...

  8. Android学习之Fragment解析

    1.定义     Fragment中文解释是碎片的意思,主要用在大屏幕设备上,例如平板电脑上,支持更加动态和灵活的UI设计.Fragment在你的应用中相当于是一个模块化和可重用的组件,因为Fragm ...

  9. Android利用碎片fragment实现底部标题栏(Github模板开源)

    在安卓开发当中,一个十分重要的布局则是底部标题栏了,拥有了底部标题栏,我们就拥有了整个软件UI开发的框架,一般而言,整个软件的布局首先就是从底部标题栏开始构建,然后再开始其他模块的编写,组成一个完善的 ...

随机推荐

  1. boost asio异步读写网络聊天程序客户端 实例详解

    boost官方文档中聊天程序实例讲解 数据包格式chat_message.hpp <pre name="code" class="cpp">< ...

  2. 【转载】MySQL之CONCAT()的用法

    mysql CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的mysql函数之一,下面就将为您详细介绍mysql CONCAT()函数,供您参考 mysql CONCAT(str1,st ...

  3. 【剑指Offer】31、从1到n整数中1出现的次数

      题目描述:   求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他 ...

  4. iterm2 快捷键设置

    单词跳转 设置option+ 左右键

  5. PHP AES cbc模式 pkcs7 128加密解密

    今天在对接一个第三方接口的时候,对方需要AES CBC模式下的加密.这里简单写一个demo class Model_Junjingbao extends Model { private static ...

  6. 3.SpringBoot整合Thymeleaf模板

    一.前言 SrpingBoot支持如JSP.Thymeleaf.FreeMarker.Mustache.Velocity等各种模板引擎,同时还为开发者提供了自定义模板扩展的支持. 使用嵌入式Servl ...

  7. 洛谷 P2634 BZOJ 2152 【模板】点分治(聪聪可可)

    题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好了,可是他们已 ...

  8. fzu 2087并查集的运用求最小生成树的等效边

    //对数组排序后,对于边相同并且边的两端不在一个集合内的一定是等效边或者必加边, //第一数数,第二合并集合 #include<stdio.h> #include<stdlib.h& ...

  9. [bzoj2600][Ioi2011]ricehub_二分

    ricehub bzoj-2600 Ioi-2011 题目大意:在数轴上有r块稻田,稻田坐标为整数.计划建造一个米仓,使得它可以收取尽量多的稻米.米仓的坐标仍需为整数.每一块权值为val的稻田距离米仓 ...

  10. ios自己定义类(UIView)代码生成简单的UITableViewCell

    因为一个项目中有大量的UITableViewCell须要书写,样式几乎相同都是 文字介绍:显示内容 这种. 自己又懒得写UITableViewCell类嫌不是必需:在方法tableView:cellF ...