实现思路

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布局例子(用按键切换碎片布局)的更多相关文章

  1. android开发中fragment获取context

    在用到fragment时无法使用.this来指定当前context内容,android开发中fragment获取context,可以使用getActivity().getApplicationCont ...

  2. Android 开发 之 Fragment 详解

    本文转载于 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragment 示例 : http ...

  3. Android开发笔记(4)——MainActivity.java文件修改&布局嵌套

    笔记链接:http://www.cnblogs.com/igoslly/p/6805020.html         笔记以开发名为CoffeeOrder的app活动为线索,介绍app如何从功能设计→ ...

  4. android开发 写一个自定义形状的按键

    步骤: 1.在drawable 文件夹中创建一个xml布局文件. 2.修改布局文件 3.在需要使用背景的按键中导入布局. 创建布局文件: 修改布局文件: <?xml version=" ...

  5. Android开发笔记(一百三十四)协调布局CoordinatorLayout

    协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...

  6. Android开发实战之拥有Material Design风格的侧滑布局

    在实现开发要求中,有需要会使用抽屉式布局,类似于QQ5.0的侧滑菜单,实现的方式有很多种,可以自定义控件,也可以使用第三方开源库. 同样的谷歌也推出了自己的侧滑组件——DrawLayout,使用方式也 ...

  7. 【Android开发日记】妙用 RelativeLayout 实现3 段布局

    在设计过程中,我们经常会遇到这样的需求: 把一条线3控制,左对齐左控制,右侧控制右对齐,中间控制,以填补剩余空间. 或者一列内放3个控件,上面的与顶部对齐,以下的沉在最底部,中间控件是弹性的.充满剩余 ...

  8. Android开发之利用ViewPager实现页面的切换(仿微信、QQ)

    这里利用ViewPager实现页面的滑动,下面直接上代码: 1.首先写一个Activity,然后将要滑动的Fragment镶嵌到写好的Activity中. Activity的布局文件:activity ...

  9. Android中通过Fragment进行简单的页面切换

    首先是activity中的布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constr ...

随机推荐

  1. Spark Hadoop Free 安装遇到的问题

    运行 ./sbin/start-master.sh : SparkCommand:/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java -cp /home/se ...

  2. Linux之用户组、文件权限详解

    用户和用户组 文件所有者(u) 一般为文件的创建者,谁创建了该文件,就天然的成为该文件的所有者 用ls ‐ahl命令可以看到文件的所有者 也可以使用chown 用户名 文件名来修改文件的所有者 用户组 ...

  3. Arduino在64位WIN7下无法安装驱动的解决办法

    1.获取权限 打开C:\Windows\System32\DriverStore\FileRepository,对着FileRepository文件夹,右键 >>属性 >>安全 ...

  4. 使用PHPMAILER实现PHP发邮件功能

    第一步: 打开网址https://github.com/PHPMailer/PHPMailer/ 下载PHPMailer,PHPMailer 需要 PHP 的 sockets 扩展支持,而登录 QQ ...

  5. ubuntu 16.04 安装VS CODE时 此软件来自第三方且可能包含非自由组件

    先安装gdebi: sudo apt-get install gdebi 然后执行: sudo apt install ./vscode.deb

  6. Git常见使用方法

    图参考:http://www.ruanyifeng.com/blog/2014/06/git_remote.html 1.GitLab配置 git config --global user.name ...

  7. 测试教程网.unittest教程.5. 实例: 找出所有是弱密码的用户

    From: http://www.testclass.net/pyunit/test_example_3/ 背景 当我们的测试数据是下面这些的时候,我们的用例是有问题的. [ {"name& ...

  8. 解决MSDE安装回滚的问题

    rem 解决MSDE安装回滚的问题.bat rem 设置为手动rem sc config "LanmanServer" start= DEMAND rem 设置为自动sc conf ...

  9. Razor语法记录

    虽然现在用着ASP.NET MVC但是cshtml使用Razor的标准形式去布局用的还是很少,这里就一点点把用到的关键点慢慢记下来,方便自己日后回忆吧! 1.将Action中返回的html字符串转换为 ...

  10. Java学习——用户电话输入显示

    编写程序:在窗口中添加组件,产生如下图形化界面:当用户输入用户名和电话后,点击显示按钮后,按下图格式显示. package cys; import java.awt.*; import java.aw ...