Android学习之Fragment

Android 3.0中引入fragment,主要为了解决不同屏幕分辩率的UI设计,使用fragment可以把屏幕划分成几块,这样便于管理,样式如微信。设计流程是建立四个fragment,根据监听事件响应,实现页面的切换,来两张效果图:

这里写图片描述

这里写图片描述

建立一个主布局与四个fragment布局

activity_main.xml代码

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

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_above="@+id/layout_chat"
        android:background="#888" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal" >

        <RelativeLayout
            android:id="@+id/layout_chat"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/wechat" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="微信"
                    android:textColor="#888" />
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout_addres"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/address_book" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="通讯录"
                    android:textColor="#888" />
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout_find"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/find" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="发现"
                    android:textColor="#888" />
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/layout_me"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:onClick="onClick" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:src="@drawable/people" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:text="我"
                    android:textColor="#888" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

fragment的代码,随意添加什么都可以(如listview等),这里只加了TextView,其他三个一样

<?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="horizontal" >


    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="暂无聊天信息"
        android:layout_marginLeft="80dp"
        android:textColor="#778"
        android:textSize="25sp"
        android:layout_gravity="center_vertical"

    />
</LinearLayout>

实现它需要FragmentManager对象与FragmentTransaction提交事务

public class MainActivity extends Activity {

    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private Fragment4 fragment4;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

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

        // 声明fragment对象
        fragmentManager = getFragmentManager();

    }

    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.layout_chat:

            ChioceItem(0);
            break;
        case R.id.layout_addres:

            ChioceItem(1);
            break;
        case R.id.layout_find:

            ChioceItem(2);
            break;
        case R.id.layout_me:

            ChioceItem(3);
            break;
        default:
            break;
        }

    }

    public void ChioceItem(int index) {

        switch (index) {
        case 0:
            fragmentTransaction = fragmentManager.beginTransaction();
            fragment1 = new Fragment1();
            fragmentTransaction.replace(R.id.fragment, fragment1);
            break;

        case 1:
            fragmentTransaction = fragmentManager.beginTransaction();
            fragment2 = new Fragment2();
            fragmentTransaction.replace(R.id.fragment, fragment2);
            break;

        case 2:
            fragmentTransaction = fragmentManager.beginTransaction();
            fragment3 = new Fragment3();
            fragmentTransaction.replace(R.id.fragment, fragment3);
            break;

        case 3:
            fragmentTransaction = fragmentManager.beginTransaction();
            fragment4 = new Fragment4();
            fragmentTransaction.replace(R.id.fragment, fragment4);
            break;
        default:
            break;

        }
        fragmentTransaction.commit();

    }

}

fragment的官方API介绍

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值