RadioButton 底部菜单制作


src 中的 MainActivity:

public class MainActivity extends Activity {
	private Button btnMenuDeal;
	private Button btnMenuNearby;
	private Button btnMenuMy;
	private Button btnMenuMore;
	private TextView tvTitle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// 初始化界面上的文字控件
		tvTitle = (TextView) findViewById(R.id.tv_title);
		// 初始化底部菜单的4个按钮
		btnMenuDeal = (Button) findViewById(R.id.btn_menu_deal);
		btnMenuNearby = (Button) findViewById(R.id.btn_menu_nearby);
		btnMenuMy = (Button) findViewById(R.id.btn_menu_my);
		btnMenuMore = (Button) findViewById(R.id.btn_menu_more);
		// 为底部菜单的4个按钮配置点击监听
		MenuButtonOnClickListener listener = new MenuButtonOnClickListener();
		btnMenuDeal.setOnClickListener(listener);
		btnMenuNearby.setOnClickListener(listener);
		btnMenuMy.setOnClickListener(listener);
		btnMenuMore.setOnClickListener(listener);
		// 默认选中第1个
		selectMenuItem(btnMenuDeal);
	}
	
	private class MenuButtonOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_menu_deal:
				selectMenuItem(btnMenuDeal);
				tvTitle.setText("团购");
				break;
				
			case R.id.btn_menu_nearby:
				selectMenuItem(btnMenuNearby);
				tvTitle.setText("附近");
				break;
				
			case R.id.btn_menu_my:
				selectMenuItem(btnMenuMy);
				tvTitle.setText("我的");
				break;
				
			case R.id.btn_menu_more:
				selectMenuItem(btnMenuMore);
				tvTitle.setText("更多");
				break;
			}
		}
		
	}
	
	/**
	 * 选择底部菜单中的某一项
	 * @param v 被选中的项
	 */
	private void selectMenuItem(View v) {
		// 实现基本思路是选中1个,取消选中3个
		// 具体实现时,可以先取消选中所有,然后选中1个
		// 取消选中所有
		btnMenuDeal.setSelected(false);
		btnMenuNearby.setSelected(false);
		btnMenuMy.setSelected(false);
		btnMenuMore.setSelected(false);
		// 选中参数对应的控件
		v.setSelected(true);
	}
}


layout 中的 activity_main.xml:
<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:background="#EEEEEE"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:paddingBottom="3dp"
        android:paddingTop="10dp" >

        <Button
            android:id="@+id/btn_menu_deal"
            style="@style/MenuItem"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/menu_deal_selector"
            android:text="团购" />

        <Button
            android:id="@+id/btn_menu_nearby"
            style="@style/MenuItem"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/menu_nearby_selector"
            android:text="附近" />

        <Button
            android:id="@+id/btn_menu_my"
            style="@style/MenuItem"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/menu_my_selector"
            android:text="我的" />

        <Button
            android:id="@+id/btn_menu_more"
            style="@style/MenuItem"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/menu_more_selector"
            android:text="更多" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="团购"
        android:textSize="32sp" />

</RelativeLayout>






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值