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 ...
随机推荐
- Hive 数据类型
1. hive的数据类型Hive的内置数据类型可以分为两大类:(1).基础数据类型:(2).复杂数据类型2. hive基本数据类型基础数据类型包括: TINYINT,SMALLINT,INT,BIGI ...
- 升级ambari、HDP版本(ambari 2.1升级到2.4、HDP2.3升级到2.5)
转载自:http://blog.csdn.net/levy_cui/article/details/52461377 官方升级版本说明 http://docs.hortonworks.com/HDPD ...
- MySQL+MyBatis下批量修改数据的问题
今天处理数据批量的更新,场景是这样子的,web站管理的字典功能,需要添加一个记录的整体描述,以及详细内容的描述.一个字典整体概述只有一组信息,但是其详细内容,会有很多项,不确定. 这个场景,在关系型数 ...
- java多线程找素数实例
package ltb20180106; public class FindPrime implements Runnable{ private int prime; private int q; p ...
- ALGO-147_蓝桥杯_算法训练_4-3水仙花数
问题描述 打印所有100至999之间的水仙花数.所谓水仙花数是指满足其各位数字立方和为该数字本身的整数,例如 =^+^+^. 样例输入 一个满足题目要求的输入范例. 例: 无 样例输出 xxx xxx ...
- C/C++基础--模板与泛型编程
模板参数 函数模板,编译器根据实参来为我们推断模板实参. 模板中可以定义非类型参数,表示一个值而非一个类型,这些值必须是常量表达式,从而允许编译器在编译时实例化模板. 非类型参数可以是整型,或者一个指 ...
- vagrant 本地添加box 支持带版本号
众所周知,vagrant添加box的时候要从外网下载,那速度...(说多了都是泪),所以只好用下载工具下载到本地之后再添加. 一般处理方案 vagrant box add boxName ./down ...
- 【剑指offer】输出链表倒数第K个元素
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ ...
- svn 被锁住 冲突 Can't revert without reverting children
解决 执行以下命令 D:\development\work>svn rm –-keep-local D:\development\work\ohho\common\logic\backstage ...
- VS2012 创建的entityframework 4.1版本
起因:部署网站提示错误“Method not found: 'Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullRe ...