Android使用Fragment仿微信底部导航栏

这是一个使用Fragment做的一个底部导航栏的小demo
MainActivity的代码
package com.example.dell.myapplication;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageButton weixin;
    private ImageButton contact;
    private ImageButton find;
    private ImageButton me;
    private ContactFragment contactFragment;
    private WeiXinFragment weiXinFragment;
    private FindFragment findFragment;
    private MeFragment meFragment;

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

        weixin = (ImageButton) findViewById(R.id.weixin1);
        contact = (ImageButton) findViewById(R.id.weixin2);
        find = (ImageButton) findViewById(R.id.weixin3);
        me = (ImageButton) findViewById(R.id.weixin4);

        weixin.setOnClickListener(this);
        contact.setOnClickListener(this);
        find.setOnClickListener(this);
        me.setOnClickListener(this);

        //该按钮点击一次
        weixin.performClick();
    }

    @Override
    public void onClick(View v) {
        cleanIcn();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (v.getId()){
            case R.id.weixin1:
                if (weiXinFragment == null) {
                    weiXinFragment = new WeiXinFragment();
                }
                transaction.replace(R.id.fragment_container,weiXinFragment);
                weixin.setImageResource(R.drawable.weixin2);
                break;
            case R.id.weixin2:
                if (contactFragment == null) {
                    contactFragment = new ContactFragment();
                }
                transaction.replace(R.id.fragment_container,contactFragment);
                contact.setImageResource(R.drawable.weixin2);
                break;
            case R.id.weixin3:
                if (findFragment == null) {
                    findFragment = new FindFragment();
                }
                transaction.replace(R.id.fragment_container,findFragment);
                find.setImageResource(R.drawable.weixin2);
                break;
            case R.id.weixin4:
                if (meFragment== null){
                    meFragment = new MeFragment();
                }
                transaction.replace(R.id.fragment_container,meFragment);
                me.setImageResource(R.drawable.weixin2);
                break;
        }
        transaction.commit();
    }


    private void cleanIcn(){
        weixin.setImageResource(R.drawable.weixin);
        contact.setImageResource(R.drawable.weixin);
        find.setImageResource(R.drawable.weixin);
        me.setImageResource(R.drawable.weixin);
    }
}
View Code

对于的activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dell.myapplication.MainActivity">

    <LinearLayout
        android:id="@+id/ll_button"
        android:gravity="center_vertical"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:layout_weight="1"
            android:id="@+id/weixin1"
            android:background="@null"
            android:src="@drawable/weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <ImageButton
            android:layout_weight="1"
            android:id="@+id/weixin2"
            android:background="@null"
            android:src="@drawable/weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <ImageButton
            android:layout_weight="1"
            android:id="@+id/weixin3"
            android:background="@null"
            android:src="@drawable/weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <ImageButton
            android:layout_weight="1"
            android:id="@+id/weixin4"
            android:background="@null"
            android:src="@drawable/weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_above="@id/ll_button"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>
</RelativeLayout>
View Code

接下来是Fragment的代码

下面是fragment的Java代码,每个Java代码都对应这一个布局

package com.example.dell.myapplication;

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

public class WeiXinFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wei_xin, container, false);
        Button button = (Button) view.findViewById(R.id.btn_weixin);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"微信",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

}
View Code
package com.example.dell.myapplication;

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


public class ContactFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact, container, false);
        Button button = (Button) view.findViewById(R.id.btn_contact);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"联系人",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

}
View Code
package com.example.dell.myapplication;

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;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by dell on 2017/8/3.
 */

public class FindFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        Button button = (Button) view.findViewById(R.id.btn_find);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"发现",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}
View Code
package com.example.dell.myapplication;

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


public class MeFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        Button button = (Button) view.findViewById(R.id.btn_me);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"我的",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

}
View Code

下面就是Java代码相应的布局代码

fragment_wei_xin.xml
<FrameLayout 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"
    tools:context="com.example.dell.myapplication.WeiXinFragment">

    <Button
        android:id="@+id/btn_weixin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="微信" />

</FrameLayout>
View Code
fragment_contact.xml
<FrameLayout 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"
    tools:context="com.example.dell.myapplication.WeiXinFragment">

    <Button
        android:id="@+id/btn_contact"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="联系人" />

</FrameLayout>
View Code
fragment_find.xml
<FrameLayout 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"
    tools:context="com.example.dell.myapplication.WeiXinFragment">

    <Button
        android:id="@+id/btn_find"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="发现" />

</FrameLayout>
View Code
fragment_me.xml
<FrameLayout 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"
    tools:context="com.example.dell.myapplication.WeiXinFragment">

    <Button
        android:id="@+id/btn_me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="我的" />

</FrameLayout>
View Code
下面是给每个Fragment添加按钮的点击事件,值得注意的是,在Fragment的点击事件跟Activity不一完全相同,在获取空间是不是直接findViewById,
而是要通过获得的view,而获取context不能直接this了,要是用getActivity()
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        //需要通过上面获得的View来findViewById
        Button button = (Button) view.findViewById(R.id.btn_me);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在这里不能使用this关键字了,要使用getActivity()代替
                Toast.makeText(getActivity(),"我的",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

效果图

 

 项目源代码:http://download.csdn.net/download/qq_33200967/9929375

 



转载于:https://www.cnblogs.com/yeyupiaoling/p/7280858.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值