布局檔案如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="fill_parent" />
- <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:background="@drawable/bottom_bg" android:layout_alignParentBottom="true" android:gravity="center_vertical" style="@android:style/ButtonBar">
- <ImageButton android:id="@+id/searchBtn" android:src="@drawable/search_on" android:background="#00000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" />
- <ImageButton android:id="@+id/historyBtn" android:src="@drawable/history" android:background="#00000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" />
- <ImageButton android:id="@+id/starredBtn" android:src="@drawable/starred" android:background="#00000000" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" />
- </LinearLayout>
- </RelativeLayout>
代碼如下:
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ImageButton;
- import android.widget.ViewFlipper;
-
- public class Search extends Activity {
-
- private ImageButton searchBtn;
- private ImageButton historyBtn;
- private ImageButton starredBtn;
- //通過ViewFlipper在多個view間轉換
- private ViewFlipper flipper;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViews();
- bindListener();
- }
-
- public void findViews(){
- searchBtn=(ImageButton)findViewById(R.id.searchBtn);
- historyBtn=(ImageButton)findViewById(R.id.historyBtn);
- starredBtn=(ImageButton)findViewById(R.id.starredBtn);
-
- flipper=(ViewFlipper)findViewById(R.id.flipper);
-
- LayoutInflater layoutInflater=this.getLayoutInflater();
- View search=layoutInflater.inflate(R.layout.search, null);
- View history=layoutInflater.inflate(R.layout.history, null);
- View starred=layoutInflater.inflate(R.layout.starred, null);
- //將3個view加入ViewFlipper中
- flipper.addView(search, 0);
- flipper.addView(history, 1);
- flipper.addView(starred, 2);
- //設定首先顯示第一個view
- flipper.setDisplayedChild(0);
- }
-
- public void bindListener(){
- searchBtn.setOnClickListener(new OnClickListener() {
- //點擊第一個button,設定view為0
- @Override
- public void onClick(View v) {
- searchBtn.setImageResource(R.drawable.search_on);
- historyBtn.setImageResource(R.drawable.history);
- starredBtn.setImageResource(R.drawable.starred);
- flipper.setDisplayedChild(0);
- }
- });
- historyBtn.setOnClickListener(new OnClickListener() {
-
//點擊第二個button,設定view為1
- @Override
- public void onClick(View v) {
- searchBtn.setImageResource(R.drawable.search);
- historyBtn.setImageResource(R.drawable.history_on);
- starredBtn.setImageResource(R.drawable.starred);
- flipper.setDisplayedChild(1);
- }
- });
- starredBtn.setOnClickListener(new OnClickListener() {
//點擊第三個button,設定view為2
- @Override
- public void onClick(View v) {
- searchBtn.setImageResource(R.drawable.search);
- historyBtn.setImageResource(R.drawable.history);
- starredBtn.setImageResource(R.drawable.starred_on);
- flipper.setDisplayedChild(2);
- }
- });
- }
- }
程式: