項目名稱:仿團購底部App
項目描述:使用LinearLayout 與 Button selector
項目功能:通過點擊按鈕,實現了底部 圖標和字體顏色的變化,
項目技術:在監聽中,判斷點擊的按鈕,並調用 setSelected(true) 選中當前點擊按鈕,調用 setSelected(false) 取消其他三個按
開發過程:
*1.聲明LinearLayout 與 Button 設計底部 Tab 菜單
* 2.聲明四個 Button
* 3.初始化四個 Button
* 4.爲四個 Button 設置監聽
* 5.在監聽中,判斷點擊的按鈕,並調用 setSelected(true) 選中當前點擊按鈕,調用 setSelected(false) 取消其他三個按鈕
selector.xml 可以直接複製上一個博客
activity_tab_menu.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"
tools:context=".TabMenuActivity" >
<LinearLayout
android:id="@+id/ll_tab_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_tab_menu_deal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawablePadding="5dp"
android:drawableTop="@drawable/deal_selector"
android:text="團購"
android:textColor="@drawable/text_color_selector" />
<Button
android:id="@+id/btn_tab_menu_nearby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawablePadding="5dp"
android:drawableTop="@drawable/nearby_selector"
android:text="附近"
android:textColor="@drawable/text_color_selector" />
<Button
android:id="@+id/btn_tab_menu_my"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawablePadding="5dp"
android:drawableTop="@drawable/my_selector"
android:text="我的"
android:textColor="@drawable/text_color_selector" />
<Button
android:id="@+id/btn_tab_menu_more"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:drawablePadding="5dp"
android:drawableTop="@drawable/more_selector"
android:text="更多"
android:textColor="@drawable/text_color_selector" />
</LinearLayout>
</RelativeLayout>
TabMenuActivity.java
package cn.sophia.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
/**
* 開發步驟:
* 1.聲明LinearLayout 與 Button 設計底部 Tab 菜單
* 2.聲明四個 Button
* 3.初始化四個 Button
* 4.爲四個 Button 設置監聽
* 5.在監聽中,判斷點擊的按鈕,並調用 setSelected(true) 選中當前點擊按鈕,調用 setSelected(false) 取消其他三個按鈕
*/
public class TabMenuActivity extends Activity implements OnClickListener{
// 聲明控件
private LinearLayout llTabMenu;
private Button btnDeal;
private Button btnNearby;
private Button btnMy;
private Button btnMore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_menu);
// 初始化控件
initViews();
// 爲控件設置監聽
btnDeal.setOnClickListener(this);
btnNearby.setOnClickListener(this);
btnMore.setOnClickListener(this);
btnMy.setOnClickListener(this);
}
/**
* 初始化控件
*/
private void initViews() {
llTabMenu = (LinearLayout) findViewById(R.id.ll_tab_menu);
btnDeal = (Button) findViewById(R.id.btn_tab_menu_deal);
btnNearby = (Button) findViewById(R.id.btn_tab_menu_nearby);
btnMy = (Button) findViewById(R.id.btn_tab_menu_my);
btnMore = (Button) findViewById(R.id.btn_tab_menu_more);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_tab_menu_deal:
btnDeal.setSelected(true);
btnMore.setSelected(false);
btnMy.setSelected(false);
btnNearby.setSelected(false);
break;
case R.id.btn_tab_menu_nearby:
btnDeal.setSelected(false);
btnMore.setSelected(false);
btnMy.setSelected(false);
btnNearby.setSelected(true);
break;
case R.id.btn_tab_menu_my:
btnDeal.setSelected(false);
btnMore.setSelected(false);
btnMy.setSelected(true);
btnNearby.setSelected(false);
break;
case R.id.btn_tab_menu_more:
btnDeal.setSelected(false);
btnMore.setSelected(true);
btnMy.setSelected(false);
btnNearby.setSelected(false);
break;
}
}
}