Fragment底部菜单栏

本文介绍了一种使用Android中的RadioGroup实现底部导航栏的方法,并详细展示了如何通过不同选项卡触发对应的Fragment显示,适用于初学者及需要快速实现底部导航功能的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

犹豫现在项目中用到的较多,而在网上搜了一些感觉都不是自己想要的,所以自己就写了一个!犹豫刚接触安卓时间不长,请打牛无视!

package com.example.bottm;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements
		OnCheckedChangeListener {

	private RadioGroup radiogroup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();// 初始化控件
	}

	/**
	 * 
	 */
	private void initView() {
		radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
		radiogroup.setOnCheckedChangeListener(this);
	}

	// 监听rediogroup的选择改变!
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (radiogroup.getCheckedRadioButtonId()) {
		case 2131099651:// 首页
			Fargmento fragment = new Fargmento();
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.fargment, fragment).commit();
			break;
		case 2131099652:// 实时监控
			Fragment1 fragment1=new Fragment1();
			getSupportFragmentManager().beginTransaction()
			.replace(R.id.fargment, fragment1).commit();
			break;
		case 2131099653:// 在线解答的界面

			break;
		case 2131099654:// 专家在线解答的界面

			break;
		case 2131099655:// 搜索的界面

			break;
		case 213109956:// 更多的界面

			break;

		default:
			break;
		}
	}

}

这一出现的一个问题就是,当我们继承activity的时候,
RadioGroup得到当前选中的对象是一个int值,而当我们继承fragmentactivity的时候就得到的是一个内存的地址,所以最终使用<pre name="code" class="java">radiogroup.getCheckedRadioButtonId()这个方法就解决的哪个问题!我底部的菜单采用的是readgroud这个控件来实现的!
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ra"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/fargment"
        android:layout_width="match_parent"
        android:layout_height="418dp"
        android:orientation="vertical" >
    </LinearLayout>

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/header_more_list"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio_index"
            style="@style/maintabt"
            android:drawableTop="@drawable/icon_1_n"
            android:text="首页" />

        <RadioButton
            android:id="@+id/radio_monotor"
            style="@style/maintabt"
            android:drawableTop="@drawable/icon_0_n"
            android:text="实时监控" />

        <RadioButton
            android:id="@+id/radio_solution"
            style="@style/maintabt"
            android:drawableTop="@drawable/icon_2_n"
            android:text="在线解答" />

        <RadioButton
            android:id="@+id/radio_expertOnline"
            style="@style/maintabt"
            android:drawableTop="@drawable/icon_3_n"
            android:text="专家在线" />

        <RadioButton
            android:id="@+id/radio_query"
            style="@style/maintabt"
            android:drawableTop="@drawable/icon_4_n"
            android:text="搜索" />

        <RadioButton
            android:id="@+id/radio_more"
            style="@style/maintabt"
            android:drawableTop="@drawable/icon_5_n"
            android:text="更多" />
    </RadioGroup>

</RelativeLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值