码迷,mamicode.com
首页 > 其他好文 > 详细

赵雅智_fragment动态添加实现

时间:2014-06-15 16:29:03      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:android   class   blog   code   java   http   

步骤

  • 新建主layout包含两个RelativeLayout布局
  • 新建2个Fragment类分别对应2个layout
  • 在主activity中进行动态添加
    • 获取FragmentManager对象
    • 获取FragmentTransaction对象
    • 添加Fragment对象
    • 提交事务
运行效果:
bubuko.com,布布扣

主要代码:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/relativeLayout1" >
    </RelativeLayout>

</RelativeLayout>


activity_fragment1.xml
<?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="wrap_content"
    android:orientation="vertical"
    android:background="#FFFF6F" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>


activity_fragment2.xml
<?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="wrap_content"
    android:orientation="vertical"
    android:background="#ffa6ff" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>


MainActivity.java
package com.example.android_fragment2;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//1.获取FragmentManager对象
		FragmentManager manager = getFragmentManager();
		//2.获取FragmentTransaction对象
		FragmentTransaction transaction = manager.beginTransaction();
		//添加Fragment对象
		transaction.add(R.id.relativeLayout1, new Fragment1(), "fragment1");
		transaction.add(R.id.relativeLayout2, new Fragment2(), "fragment2");
		//4.提交
		transaction.commit();
	}
 

}


Fragment1.java
package com.example.android_fragment2;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_fragment1, container, false);
	}

}


Fragment2.java
package com.example.android_fragment2;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.activity_fragment2, container, false);
	}

}




赵雅智_fragment动态添加实现,布布扣,bubuko.com

赵雅智_fragment动态添加实现

标签:android   class   blog   code   java   http   

原文地址:http://blog.csdn.net/zhaoyazhi2129/article/details/30460581

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!