<?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:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is another fragment"
android:textSize="30sp"
android:layout_gravity="center"/> </LinearLayout>

package com.example.fragmenttest;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by Administrator on 2016/3/2.
*/
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;
}
}

<?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.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/> <FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/right_layout"> <fragment
android:id="@+id/right_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.fragmenttest.RightFragment"/> </FrameLayout> </LinearLayout>

package com.example.fragmenttest;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener{ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
AnotherRightFragment fragment = new AnotherRightFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
         //模拟返回栈
         //transaction.addToBackStack(null);
transaction.commit();
break;
default:
break;
} }
}



												

Android — — —动态添加碎片的更多相关文章

  1. Android动态添加碎片

    我们编写一个能够用过按钮动态更替碎片的APP,首先在主页上显示第一个碎片,点击按钮后可以替换到第二个碎片,或者删除已经替换掉的第二个碎片. 一.MainActivity.java import and ...

  2. Android中如何动态添加碎片

    Android中的开发需要兼容手机和平板,两个方面.这就引入了碎片的概念.(注意:这里用的Fragment强烈建议使用support-v4库中的Fragment) 碎片:是一种可以嵌入在活动当中的UI ...

  3. Android动态添加Device Admin权限

    /********************************************************************** * Android动态添加Device Admin权限 ...

  4. Android 动态添加Spinner(.java文件内实现) 实现 改变spinner 内文字属性

    动态添加spinner 控件 Spinner s = new Spinner(this); String []items={"自己定义的要显示的数组"}; my_SpinnerAd ...

  5. Android动态添加和移除布局

    package com.hyang.administrator.studentproject; import android.os.Bundle; import android.support.v7. ...

  6. android动态添加TextView或者ImageView

    动态添加 text1=new TextView(this); text1.setText("动态添加"); ((LinearLayout) this.findViewById(R. ...

  7. Android -- 动态添加布局

    在做项目的时候,遇到了scrollView与listView结合的使用,导致了滑动的混乱,但是有一个办法可以解决掉这个问题,就是手写listView的高度,还有另外一种方法,传送门:<Andro ...

  8. Android 动态添加删除ExpandableListView的item的例子

    这个例子可以学习到如下几点: 1.通过自定义Dialog(单独布局的xml文件进行弹出显示) 2.通过menu点击监听添加,删除view中的items 3.点击ExpandableListView中g ...

  9. Android动态添加布局

    //1.利用LayoutInflater的inflate动态加载XML mLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID) ...

随机推荐

  1. django 模型

    一.project 与app之间的关系 1个project中可包含多个app eg:包含两个app的project的结构

  2. 配置sublime text 3 的Python开发环境

    为了在sublime实现像Python自带idle一样的F5交互功能: 首先安装package control然后install Sublime REPL:然后配置 Preference-->P ...

  3. Reconstruction

    /* Reconstruction */    /*@mixin border($position){      @if $position == left-right or $position == ...

  4. Cognos10安装注意事项

    cognos10用db2做content management注意事项 1. 建议用UTF-8格式字符2. 建议pagesize用8K或者8K以上3. 新建数据库缓冲池pagesize和以上1.2设置 ...

  5. loadrunner随笔1

    快捷键: 注释快捷键:  ctrl+alt+c 取消注释快捷键:ctrl+alt+u 开始录制快捷键: ctrl+r 运行时设置快捷键: f4 录制设置: ctrl + f7 查找和替换:ctrl + ...

  6. iPhone5停留在语音的界面,提示按三次home键,无法继续下去

    不知道之前用户是怎么操作的,可能是刷机或恢复出厂设置.穷人,没用玩过iPhone. 根据提示关键词,网上搜索,发现只需要按三次home,三次锁屏,三次锁屏,最后再三次home就可以了. 试了两次,还真 ...

  7. YY前端课程-自习

    1. 默认的浏览器字体 100% = 1em  =1rem =16px  =12pt em继承父元素,rem只继承html根元素 2. text-align水平对齐影响一个元素中文本的水平对齐方式,控 ...

  8. SSM框架学习之高并发秒杀业务--笔记5-- 并发优化

    前几节终于实现了这个高并发秒杀业务,现在问题是如何优化这个业务使其能扛住一定程度的并发量. 一. 优化分析 对于整个业务来说,首先是分析哪些地方会出现高并发,以及哪些地方会影响到了业务的性能.可能会出 ...

  9. [原创]Win7、Win8、Win10始终以管理员身份运行程序。

    在Win7.Win8.Win10系统中,以管理员身份运行程序很麻烦,一般有以下几种方式: 1.在可执行程序或快捷方式上右键,以管理员身份运行: 2.在可执行程序或快捷方式上右键->属性-> ...

  10. Spark机器学习示例

    1. Java代码 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor lice ...