fragement练习:微信切换

微信切换

1.项目目录
在这里插入图片描述

xml:activity_mai
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

   <fragment
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/fg"
       android:name="com.example.acer.fragementwx.WeiChat_fragement"></fragment>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/l"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/image1"
            android:src="@drawable/weixin"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/image2"
            android:src="@drawable/faxian"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/image3"
            android:src="@drawable/wo"/>
        </LinearLayout>

</RelativeLayout>
xml:faxian
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/fx_bottom"
    android:scaleType="fitXY"/>
</RelativeLayout>
xml:wechat
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/wx_bottom"
    android:scaleType="fitXY"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>
xml:wo
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/w_ottom"
        android:scaleType="fitXY"/>
</RelativeLayout>
main activity
package com.example.acer.fragementwx;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView1 = (ImageView) findViewById(R.id.image1);
        ImageView imageView2 = (ImageView) findViewById(R.id.image2);
        ImageView imageView3 = (ImageView) findViewById(R.id.image3);
        imageView1.setOnClickListener(l);   //为组件设置单击事件
        imageView2.setOnClickListener(l);
        imageView3.setOnClickListener(l);
    }
        View.OnClickListener l=new View.OnClickListener() { //编写单击事件监听器
            @Override
            public void onClick(View v) {
                FragmentManager fm=getFragmentManager();    //创建 FragmentManager
                FragmentTransaction ft=fm.beginTransaction();
                Fragment f=null;
                switch (v.getId()){
                    case R.id.image1:
                        f=new WeiChat_fragement();
                        break;
                    case R.id.image2:
                        f=new faxian_fragement();
                        break;
                    case R.id.image3:
                        f=new wo_fragement();
                        break;
                    default:
                        break;
                }
                ft.replace(R.id.fg,f);  //替换fragement,要显示的容器,创建的对象
                ft.commit();
            }
        };
    }
WeiChat_fragement:
package com.example.acer.fragementwx;

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


public class WeiChat_fragement extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.wechat_fragement,null);
        return view;
    }
}

faxian_fragement:
package com.example.acer.fragementwx;

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

/**
 * Created by acer on 2019/12/7.
 */
public class faxian_fragement extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fanxian_fragement,null);
        return view;
    }
}


wo_fragement:
package com.example.acer.fragementwx;

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

/**
 * Created by acer on 2019/12/7.
 */
public class wo_fragement extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.wo_fragement,null);
        return view;
    }
}

运行结果:
点击切换
在这里插入图片描述

发布了55 篇原创文章 · 获赞 5 · 访问量 4147

猜你喜欢

转载自blog.csdn.net/qq_43654669/article/details/103495217