<?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. js 数组赋值问题 :值传递还是引用?

    转载于知乎var a = [1,2,3]; var b = a; a = [4,5,6]; alert(b); //[1,2,3] 面试时被问到这样一个问题,竟然从来没试过... 当时直接的理解,数组 ...

  2. MySQL DDL 整理

    DDL is Data Definition Language statements. Some examples:数据定义语言,用于定义和管理 SQL 数据库中的所有对象的语言 -- 清空表内容 T ...

  3. Python anaconda links to GOMP_4.0 and throws error

    ImportError: /usr/progtools/anaconda2/bin/../lib/libgomp.so.1: version `GOMP_4.0' not found (require ...

  4. SPSS数据分析—广义线性混合模型

    广义线性混合模型是目前线性模型范畴内最为完备的模型框架,它是广义线性模型的进一步延伸,进一步突破适用条件,因变量既 可以非正态,也可以非独立,由于其最为复杂,因此SPSS对其输出结果采用模型格式,而不 ...

  5. angularjs不同页面间参数的传递

    1.在路由中定义要接收的参数 .state('userDetails', { url: '/userDetails?phone', //以?为标识接收参数 templateUrl: 'assets/v ...

  6. Linux中Screen命令使用方法

    一.使用Screen创建一个Session screen -S sessionName 注:sessionName是要删除的session名字 二.结束一个Screen创建的session 1.首先使 ...

  7. 当执行太多不受信任的代码时,除去令人讨厌的大量 trycatch 的办法

    如果方法 Condition 返回 true,则执行 IfTrue,否则执行 IfFalse,发生异常则执行 IfError. 通常是这样: try { if (Condition()) { try ...

  8. TcpListener 类

    构造函数       名称 说明 TcpListener(IPAddress, Int32) 新实例初始化 TcpListener 类用于侦听传入的连接尝试在指定的本地 IP 地址和端口号. TcpL ...

  9. VRML开发环境配置

    Vrml真的是个好老的语言啊~~~~ 1. 安装xp虚拟机 vm已经安装过.现在安装xp系统. 发现个网站,msdn itellyou,这里能够下载很多不同的操作系统,都是纯净版的. 选择xp系统:h ...

  10. PHP global 关键字

    global关键字用于在函数内部访问全局变量. <?php $x = 5; $y = 10; function myTest(){ global $x,$y; $x = $x+$y; } myT ...