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>