編輯:關於 Android 程式設計
前面的文章已經講完「隨手拍」專案的圖像處理部分,本篇聚焦主介面的佈局與頁面滑動切換。參考鴻洋的影片教學與郭霖的《第一行代碼》(兩位部落格都強烈推薦),完成了以下內容:
(1) 學會使用 include 佈局標籤
(2) 透過 Adapter 載入 Fragment
(3) 用 ViewPager 實作滑動切換頁面
(4) 切換時同步改變圖示與背景,並處理 Fragment 內控件事件與參數傳遞
如下圖,滑動螢幕可在「空間」、「相冊」、「關注」之間切換,同時圖示變色、背景加深。

另外加入了按鈕事件:Fragment1 點擊後顯示文字,Fragment3 點擊後抓取 Fragment2 的內容並呈現。


首先是頂部佈局 top_layout.xml,採相對佈局,右側放兩個圖示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:background="@drawable/image_toolbar_bg" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/icon_suishoupai" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="隨手拍"
android:textSize="15sp"
android:layout_gravity="center"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_alignParentRight="true"
android:orientation="horizontal" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/image_top_watch" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/image_top_add" />
</LinearLayout>
</RelativeLayout>
接著是底部佈局 bottom_layout.xml,由三個水平 LinearLayout 組成,每個包含 ImageView 與 TextView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/image_toolbar_bg"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/bottomLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:background="@drawable/image_toolbar_bg_sel"
android:orientation="vertical" >
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:src="@drawable/image_bottom_effect" />
<TextView
android:layout_width="wrap_content"
android:layout_height="15dp"
android:text="空間"
android:textColor="#ffffff"
android:textSize="10dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottomLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:src="@drawable/image_bottom_frame_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="15dp"
android:text="相冊"
android:textColor="#ffffff"
android:textSize="10dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottomLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:src="@drawable/image_bottom_person_no" />
<TextView
android:layout_width="wrap_content"
android:layout_height="15dp"
android:text="關注"
android:textColor="#ffffff"
android:textSize="10dp" />
</LinearLayout>
</LinearLayout>
最後在 activity_main.xml 中用 include 引入這兩個佈局。此處暫不放入 ViewPager,ViewPager 稍後會加進來承載不同 Fragment 並實現觸屏切換:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/top_layout" />
<include layout="@layout/bottom_layout" />
</LinearLayout>
MainActivity 的 onCreate 中呼叫 requestWindowFeature(Window.FEATURE_NO_TITLE) 即可去掉標題列:

先寫 Fragment 的佈局,fragment_layout1.xml 如下,其餘類似:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:gravity="center"
android:text="The First Fragment" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1" />
</LinearLayout>
接著建立 FragmentFirst、FragmentSecond、FragmentThird,以 FragmentSecond 為例:
package com.example.layouttest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentSecond extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout2, container, false);
}
}
小技巧:在 Eclipse 新建類別時,可以點「瀏覽」自訂要繼承的超類,或點「添加」增加介面,這裡繼承 Fragment。注意 import android.support.v4.app.Fragment; 要前後一致。

MainActivity 的程式碼:
package com.example.layouttest;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Window;
public class MainActivity extends FragmentActivity {
// 注意:導入時均為 support.v4 保持一致
private ViewPager viewPager1;
private FragmentPagerAdapter fpAdapter;
private List<Fragment> listData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設置無標題需要在 setContentView 前調用,否則會崩潰
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setViewPager();
}
private void setViewPager() {
viewPager1 = (ViewPager) findViewById(R.id.viewpager1);
listData = new ArrayList<Fragment>();
FragmentFirst fragmentFirst = new FragmentFirst();
FragmentSecond fragmentSecond = new FragmentSecond();
FragmentThird fragmentThird = new FragmentThird();
listData.add(fragmentFirst);
listData.add(fragmentSecond);
listData.add(fragmentThird);
// ViewPager 相當於一個容器組件,實現頁面切換
fpAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public int getCount() {
return listData.size();
}
@Override
public Fragment getItem(int arg0) {
return listData.get(arg0);
}
};
viewPager1.setAdapter(fpAdapter);
}
}
此時滑動切換已可運作,但有兩點要注意:
(1) MainActivity 要從 Activity 改為繼承 FragmentActivity。
(2) 若遇到「類型對於參數 FragmentFirst 不適用」的錯誤,把 import 改成 android.support.v4.app.Fragment,並確認 support.v4 的 app 與 view 套件保持一致。
先宣告底部圖示與容器:
// 底部圖標 private ImageView image1; private ImageView image2; private ImageView image3; private LinearLayout layout1; private LinearLayout layout2; private LinearLayout layout3;
在 setViewPager() 中,於 viewPager1.setAdapter(fpAdapter) 之後加上監聽:
// 初始化圖標
image1 = (ImageView) findViewById(R.id.image1);
image2 = (ImageView) findViewById(R.id.image2);
image3 = (ImageView) findViewById(R.id.image3);
layout1 = (LinearLayout) findViewById(R.id.bottomLayout1);
layout2 = (LinearLayout) findViewById(R.id.bottomLayout2);
layout3 = (LinearLayout) findViewById(R.id.bottomLayout3);
viewPager1.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
image1.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_effect));
image2.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_frame_no));
image3.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_person_no));
layout1.setBackgroundResource(R.drawable.image_toolbar_bg_sel);
layout2.setBackgroundResource(R.drawable.image_toolbar_bg);
layout3.setBackgroundResource(R.drawable.image_toolbar_bg);
break;
case 1:
image1.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_effect_no));
image2.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_frame));
image3.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_person_no));
layout1.setBackgroundResource(R.drawable.image_toolbar_bg);
layout2.setBackgroundResource(R.drawable.image_toolbar_bg_sel);
layout3.setBackgroundResource(R.drawable.image_toolbar_bg);
break;
case 2:
image1.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_effect_no));
image2.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_frame_no));
image3.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_person));
layout1.setBackgroundResource(R.drawable.image_toolbar_bg);
layout2.setBackgroundResource(R.drawable.image_toolbar_bg);
layout3.setBackgroundResource(R.drawable.image_toolbar_bg_sel);
break;
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
@Override
public void onPageScrollStateChanged(int arg0) { }
});
switch 中的 0、1、2 對應 listData 中裝入的三個 Fragment 順序。
FragmentFirst 透過 onActivityCreated() 掛上點擊事件:
public class FragmentFirst extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout1, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// 添加 Fragment1 的響應事件
Button button1 = (Button) getActivity().findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView textView1 = (TextView) getActivity().findViewById(R.id.textView1);
textView1.setText("在fragment1中點擊按鈕");
}
});
}
}
FragmentThird 則抓取 Fragment2 的資料顯示:
public class FragmentThird extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout3, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// 添加 Fragment3 的響應事件
Button button3 = (Button) getActivity().findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView textView2 = (TextView) getActivity().findViewById(R.id.textView2);
TextView textView3 = (TextView) getActivity().findViewById(R.id.textView3);
textView3.setText("點擊按鈕獲取fragment2信息:\n" + textView2.getText());
}
});
}
}
踩坑提醒:不同 Fragment 的 XML 中,TextView 的 id 要區分開。如果 Fragment1 與 Fragment2 使用相同的 textView1,程式會找不到對應控件而沒有反應。
本文重點是使用 include 拆分佈局、以 Fragment 切換頁面、用 ViewPager 實作滑動效果。內容偏基礎,適合 Android 初學者參考;若有不足之處歡迎指正。
用 TabLayout 實現京東商品詳情頁效果
早在 2015 年的 Google I/O 大會上,Google 就帶來了更為完整的 Material Design 設計規範,同時推出了全新的 Android Desi
自定義 View 的三種實現方式:自繪、組合與繼承(深入 View 系列完結篇)
不知不覺間,「帶你一步步深入了解 View」系列已經寫到第四篇。回顧前面三篇,我們談過 LayoutInflater 的原理、視圖的繪製流程,以及視圖狀態與重繪機制,Vi
Android 仿微信 QQ 頭像裁剪與圓形頭像實作
許多包含使用者系統的 App 都會需要更換頭像功能。常見流程是從相機拍照或相簿選圖,再呼叫系統裁剪工具,最後將結果顯示為圓形頭像並保存。 整體流程 使用者點擊更換頭像
Android 打造 iOS 風格的分段選擇控件 UISegmentedControl
在 iOS 7 中出現過一種扁平風格的控件——分段選擇控件 UISegmentedControl。它由幾個被細線分隔的按鈕橫放或豎排而成,每次點擊都能切換不同的按鈕與對應