FrameLayout+LinearLayout实现首页底部菜单

1.布局样式

 

 

 

 2.main.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="match_parent"
    android:orientation="vertical">

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

    </FrameLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/shortcut_icon" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:gravity="center"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <TextView
                android:id="@+id/llHomeATHome"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:drawableLeft="@drawable/phone_selector_one"
                android:drawablePadding="3dp"
                android:gravity="center_vertical"
                android:text="首页"
                android:textColor="@drawable/phone_color_bottom_tv"
                android:textSize="11sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <TextView
                android:id="@+id/llHomeATNotice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:drawableLeft="@drawable/phone_selector_two"
                android:drawablePadding="3dp"
                android:gravity="center_vertical"
                android:text="通知"
                android:textColor="@drawable/phone_color_bottom_tv"
                android:textSize="11sp" />

            <TextView
                android:id="@+id/tvNotice"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_marginStart="-40dp"
                android:layout_marginTop="-5dp"
                android:layout_toEndOf="@+id/llHomeATNotice"
                android:background="@drawable/phone_login_button_bg_normal"
                android:gravity="center"
                android:text="1"
                android:textColor="@color/white"
                android:textSize="8sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <TextView
                android:id="@+id/llHomeATMy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:drawableLeft="@drawable/phone_selector_four"
                android:drawablePadding="3dp"
                android:gravity="center_vertical"
                android:text="我的"
                android:textColor="@drawable/phone_color_bottom_tv"
                android:textSize="11sp" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

3.activity功能实现

public class MainActivity extends AppCompatActivity {

    MainActivityBinding binding;

    HomeFragment mHomeFragment =  HomeFragment.newInstance();
    NoticeFragment mNoticeFragment = NoticeFragment.newInstance();
    MineFragment mMineFragment = MineFragment.newInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = MainActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.llHomeATMy.setOnClickListener(mClickListener);//为第一个导航图片添加单机事件
        binding.llHomeATHome.setOnClickListener(mClickListener);//为第二个导航图片添加单机事件
        binding.llHomeATNotice.setOnClickListener(mClickListener);//为第三个导航图片添加单机事件

        FragmentManager fm = getSupportFragmentManager();//获取Fragment的管理器
        FragmentTransaction transaction = fm.beginTransaction();// 开启一个事务
        transaction.add(R.id.flHomeFragment, mHomeFragment).commit();//设置初始的fragment
        binding.llHomeATHome.setSelected(true);
    }

    //创建单机事件监听器
    View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();//获取Fragment的管理器
            FragmentTransaction transaction = fm.beginTransaction();// 开启一个事务
            int id = v.getId();//通过获取点击的id判断点击了哪个张图片
            if (id == R.id.llHomeATHome) {
                transaction.replace(R.id.flHomeFragment, mHomeFragment); //替换Fragment
                binding.llHomeATHome.setSelected(true);
                binding.llHomeATNotice.setSelected(false);
                binding.llHomeATMy.setSelected(false);
            } else if (id == R.id.llHomeATNotice) {
                transaction.replace(R.id.flHomeFragment, mNoticeFragment); //替换Fragment
                binding.llHomeATNotice.setSelected(true);
                binding.llHomeATHome.setSelected(false);
                binding.llHomeATMy.setSelected(false);
            } else if (id == R.id.llHomeATMy) {
                transaction.replace(R.id.flHomeFragment, mMineFragment); //替换Fragment
                binding.llHomeATMy.setSelected(true);
                binding.llHomeATNotice.setSelected(false);
                binding.llHomeATHome.setSelected(false);
            }
            transaction.commit(); //提交事务
        }
    };

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值