Android之Fragment创建方式

a:静态添加
1)、创建Fragment继承V4包下的Fragment,添加布局创建视图
2)、在Activity的布局中使用fragment标签添加Fragment
class 或者 android:name
3)、修改Activity继承V4包下的FragmentActivity

b:动态添加
1)、创建Fragment继承V4包下的Fragment,添加布局创建视图
2)、修改Activity继承V4包下的FragmentActivity
3)、在Activity中,获得V4包下的FragmentManager
getSupportFragmentManager

静态添加
main_xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/ft_left"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.example.day12_fragment01.LeftFragment"
/>
<fragment
android:id="@+id/ft_right"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.day12_fragment01.RightFragment"
/>
左边framgment布局

<?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"
android:orientation="vertical"
android:background="#ff0">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我在左边"
/>
</LinearLayout>

右边的fagment

<?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"
android:orientation="vertical"
android:background="#f00"
>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我在右边"
/>
</LinearLayout>

代码main

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

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

public class LeftFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.left, null);
}
}

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

public class RightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.right, null);

    }
}

动态添加

main_xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/ll_left"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:background="#f00"
        android:orientation="horizontal"></LinearLayout>
    <LinearLayout
        android:id="@+id/ll_right"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:background="#0f0"
        android:layout_height="match_parent"
        android:orientation="horizontal"></LinearLayout>
</LinearLayout>

left_fragment

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

public class MainActivity extends Activity {

    @SuppressLint("CommitTransaction")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 碎片管理器,用来Fragment和Activity之间交互的一个接口
        FragmentManager manager = getFragmentManager();
        // 开启Fragment事务,事务提供三个可用方法, 添加(add),移除(remove),替换(replace)
        FragmentTransaction transaction = manager.beginTransaction();

        // 将Fragment添加到指定容器(指定的位置)
        transaction.add(R.id.ll_left, new LeftFragment());
        transaction.add(R.id.ll_right, new RightFragment());

        // 提交事务,保存
        transaction.commit();

    }
}

—–LeftFragment

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

public class LeftFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.left, null);
    }
}

------RightFragment

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

public class RightFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.right, null);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值