Android — — —动态添加碎片
2016-03-02 14:07
387 查看
<?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 ViewDragHelper介绍
- android-Service和Thread的区别
- Android动画原理分析
- kotlin第一次运行在Android上
- Android-设置PullToRefresh下拉刷新样式
- Android中Webview运行js脚本获取定位信息(Geolocation)
- android内存泄露
- Android Selector用法小记
- ResourcesUtil. getIdentifier
- android如何完全root
- Android自定义View(七)--很low的bannerView
- 解决Android 5.0以上版本Button自带阴影效果的方法
- Android学习之异步任务Handler
- 人一生最可怕的是碌碌无为,还安慰自己平凡的可贵
- Android createSurface
- 深入理解Dalvik虚拟机- Android进程启动过程
- Android中使用Handler造成内存泄露的分析和解决
- android memo--keystore
- Android Framework概述
- Android内存泄漏总结