android 开发 碎片Fragment布局例子(用按键切换碎片布局)
实现思路
1.写一个父类布局,里面写一个按键和一个帧布局(用于给Fragment布局后续替代)
2.写3个子布局,并且在写3个class继承Fragment布局
3.在MainActivity的class中写替换碎片布局的方法
(包含:FragmentManger(碎片管理器)、getSupportFragmentManager(得到支持碎片管理器)、FragmenTransaction(碎片交换器)、beginTransaction(开始碎片交换)、replace(替换)、commit(交付))
写一个父类布局,里面写一个按键和一个帧布局
<?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/Button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="改变下面的碎片"/>
<FrameLayout
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8">
</FrameLayout> </LinearLayout>
写3个子布局,并且在写3个class继承Fragment布局
<?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:padding="10dp">
<ImageView
android:id="@+id/fragment_1_ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ace"/>
<TextView
android:id="@+id/fragment_1_TextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/ace"
android:textColor="@color/colorBlack"
android:layout_marginTop="20dp"/>
</LinearLayout>
相同布局在写2个
写一个class继承fragment实现碎片布局实例化
package com.example.lenovo.myfragmentdemo2.fragment;
// 注意Fragment的依赖库建议统一使用support-v4库中的
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.lenovo.myfragmentdemo2.R; /**
* Created by lenovo on 2018/5/7.
*/ public class Fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1,container,false);
return view;
}
}
在MainActivity的class中写替换碎片布局的方法
package com.example.lenovo.myfragmentdemo2;
// 注意Fragment的依赖库
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; import com.example.lenovo.myfragmentdemo2.fragment.Fragment1;
import com.example.lenovo.myfragmentdemo2.fragment.Fragment2;
import com.example.lenovo.myfragmentdemo2.fragment.Fragment3; public class MainActivity extends AppCompatActivity {
private Button button;
private int i = 1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.Button1); button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (i){
case 1:
replaceFragment(new Fragment1());
i++;
break;
case 2:
replaceFragment(new Fragment2());
i++;
break;
case 3:
replaceFragment(new Fragment3());
i++;
break;
default:
replaceFragment(new Fragment1());
i=1;
break;
} }
});
} public void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();//注意!每次要执行commit()方法的时候都需要重新获取一次FragmentTransaction,否则用已经commit过的FragmentTransaction再次commit会报错!
transaction.replace(R.id.FrameLayout1,fragment);
transaction.commit(); }
}
运行效果:
点击后:
android 开发 碎片Fragment布局例子(用按键切换碎片布局)的更多相关文章
- android开发中fragment获取context
在用到fragment时无法使用.this来指定当前context内容,android开发中fragment获取context,可以使用getActivity().getApplicationCont ...
- Android 开发 之 Fragment 详解
本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...
- Android开发笔记(4)——MainActivity.java文件修改&布局嵌套
笔记链接:http://www.cnblogs.com/igoslly/p/6805020.html 笔记以开发名为CoffeeOrder的app活动为线索,介绍app如何从功能设计→ ...
- android开发 写一个自定义形状的按键
步骤: 1.在drawable 文件夹中创建一个xml布局文件. 2.修改布局文件 3.在需要使用背景的按键中导入布局. 创建布局文件: 修改布局文件: <?xml version=" ...
- Android开发笔记(一百三十四)协调布局CoordinatorLayout
协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...
- Android开发实战之拥有Material Design风格的侧滑布局
在实现开发要求中,有需要会使用抽屉式布局,类似于QQ5.0的侧滑菜单,实现的方式有很多种,可以自定义控件,也可以使用第三方开源库. 同样的谷歌也推出了自己的侧滑组件——DrawLayout,使用方式也 ...
- 【Android开发日记】妙用 RelativeLayout 实现3
段布局
在设计过程中,我们经常会遇到这样的需求: 把一条线3控制,左对齐左控制,右侧控制右对齐,中间控制,以填补剩余空间. 或者一列内放3个控件,上面的与顶部对齐,以下的沉在最底部,中间控件是弹性的.充满剩余 ...
- Android开发之利用ViewPager实现页面的切换(仿微信、QQ)
这里利用ViewPager实现页面的滑动,下面直接上代码: 1.首先写一个Activity,然后将要滑动的Fragment镶嵌到写好的Activity中. Activity的布局文件:activity ...
- Android中通过Fragment进行简单的页面切换
首先是activity中的布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constr ...
随机推荐
- nginx支持android、ios、微信扫一扫
首先做一个android下载的html页面,页面中识别微信浏览器提示在浏览器中打开,然后在nginx对ios进行识别并跳转到apple store #下载App location ^~ /appDow ...
- 通过状态机来对axi_lite总线进行操作
通过状态机来对axi_lite总线进行操作 状态跳转: 1.初始状态 将axi_lite读写两个信道分开进行控制,在初始状态,就根据读,写信号来判断应该跳转到那一个状态. 2.写状态 在写状态中不需要 ...
- 了解ARM+Android
第一部分 认识ARM,方案商,GPU , 芯片 1.1 ARM ARM(Advanced RISC Machines)是微处理器行业的一家知名企业,设计了大量高性能.廉价.耗能低的RISC处理器.相关 ...
- Ubuntu Docker安装
docker容器改变apt-get源 记得先:apt-get update 进入容器 cd /etc/apt echo "">sources.list echo " ...
- 黄聪:PHP调试显示所有错误信息
ini_set('display_errors',1); //错误信息 ini_set('display_startup_errors',1); //php启动错误信息 error_reporting ...
- idea14导入eclipse项目并部署运行完整步骤
idea14导入eclipse项目并部署运行完整步骤 2015年05月12日 14:08:04 阅读数:40456 首先说明一下:idea里的project相当于eclipse里的workspace, ...
- git 查看提交的信息diff
git log --stat git show <hashcode> <filename> git log --pretty=oneline <filename> ...
- DS树+图综合练习--构建邻接表
题目描述 已知一有向图,构建该图对应的邻接表.邻接表包含数组和单链表两种数据结构,其中每个数组元素也是单链表的头结点,数组元素包含两个属性,属性一是顶点编号info,属性二是指针域next指向与它相连 ...
- mysql 事务学习
1.事务 逻辑上的一组操作,组成这组操作的各个逻辑单元要么一起成功,要么一起失败. 2.事务特性 原子性:强调事务的不可分割.一致性:强调的是事务的执行的前后,数据的完整性要保持一致.隔离性:一个事务 ...
- mysql备份学习笔记及xtrabackup安装
(参考书籍:<深入浅出MySQL>) 一.备份恢复策略 a) 确定要备份的表的存储引擎是事务型还是非事务型 b) 确定使用全备份还是增量备份 c) 定期做备份 ...