編輯:關於 Android 程式設計
TabLayout 是許多人開發 Android 應用時經常會用到的組件,本文就帶大家一起動手玩轉這個官方選項卡控件,希望對正在學習的朋友有所幫助。
過去在我們搭配 ViewPager 使用的時候,往往需要借助 TabPageIndicator 來呈現漂亮的頁籤切換效果。無奈 TabPageIndicator 屬於第三方庫,而且已經有些年頭了。雖然不少高手早已自己動手改寫,但在 2015 年的 Google 大會上,官方推出了全新的 Android Support Design 庫,其中就包含了 TabLayout 這個新控件。它能輕鬆實現 TabPageIndicator 的視覺效果,而且是官方出品,最貼心的是它向下兼容到 2.2 版本。下面就用一個簡單的例子來實際體驗一下。
這裡採用 Android Studio 進行開發,因此引入 TabLayout 非常省事,只需在 build.gradle 中加入下面這行即可:
compile 'com.android.support:design:22.2.0'
由於這個示例是仿照知乎的風格做的,所以頁面整體看上去會和知乎很像。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab_FindFragment_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/titleBlue"
app:tabIndicatorColor="@color/white"
app:tabSelectedTextColor="@color/gray"
app:tabTextColor="@color/white" />
<android.support.v4.view.ViewPager
android:id="@+id/vp_FindFragment_pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
這段布局並沒有什麼特別的地方,就是在上下分別放置了一個 TabLayout 和一個 ViewPager。其中幾個關鍵屬性如下:
app:tabIndicatorColor="@color/white" // 下方滾動的下劃線顏色 app:tabSelectedTextColor="@color/gray" // tab 被選中後文字的顏色 app:tabTextColor="@color/white" // tab 默認的文字顏色
Find_tab_Adapter.java 是 ViewPager 的適配器。由於每個欄目下都會對應一些列表,若用 list<View> 的方式在其中頻繁切換布局並不合適,因此這裡採用 List<Fragment> 的方式,直接加載多個 Fragment。
package com.example.cg.myzhihu.Adapters;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class Find_tab_Adapter extends FragmentPagerAdapter {
private List<Fragment> list_fragment; // fragment 列表
private List<String> list_Title; // tab 名的列表
public Find_tab_Adapter(FragmentManager fm, List<Fragment> list_fragment, List<String> list_Title) {
super(fm);
this.list_fragment = list_fragment;
this.list_Title = list_Title;
}
@Override
public Fragment getItem(int position) {
return list_fragment.get(position);
}
@Override
public int getCount() {
return list_Title.size();
}
// 此方法用來顯示 tab 上的名字
@Override
public CharSequence getPageTitle(int position) {
return list_Title.get(position % list_Title.size());
}
}
FindFragment.java 的完整邏輯都寫在註釋裡了,重點是初始化各個控件、把 Fragment 裝進列表、添加 tab 標題,最後用 setupWithViewPager 完成綁定。
package com.example.cg.myzhihu;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.cg.myzhihu.Adapters.Find_tab_Adapter;
import java.util.ArrayList;
import java.util.List;
/** 發現頁面 */
public class FindFragment extends Fragment {
private TabLayout tab_FindFragment_title; // 定義 TabLayout
private ViewPager vp_FindFragment_pager; // 定義 ViewPager
private FragmentPagerAdapter fAdapter; // 定義 adapter
private List<Fragment> list_fragment; // 用來裝 fragment 的列表
private List<String> list_title; // tab 名稱列表
private Find_hotRecommendFragment hotRecommendFragment; // 熱門推薦
private Find_hotCollectionFragment hotCollectionFragment; // 熱門收藏
private Find_hotMonthFragment hotMonthFragment; // 本月熱榜
private Find_hotToday hotToday; // 今日熱榜
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_find, container, false);
initControls(view);
return view;
}
/** 初始化各控件 */
private void initControls(View view) {
tab_FindFragment_title = (TabLayout) view.findViewById(R.id.tab_FindFragment_title);
vp_FindFragment_pager = (ViewPager) view.findViewById(R.id.vp_FindFragment_pager);
// 初始化各個 fragment
hotRecommendFragment = new Find_hotRecommendFragment();
hotCollectionFragment = new Find_hotCollectionFragment();
hotMonthFragment = new Find_hotMonthFragment();
hotToday = new Find_hotToday();
// 將 fragment 裝進列表
list_fragment = new ArrayList<>();
list_fragment.add(hotRecommendFragment);
list_fragment.add(hotCollectionFragment);
list_fragment.add(hotMonthFragment);
list_fragment.add(hotToday);
// 加載 tab 名字,正規做法是先在 values/arrays.xml 中定義再調用
list_title = new ArrayList<>();
list_title.add("熱門推薦");
list_title.add("熱門收藏");
list_title.add("本月熱榜");
list_title.add("今日熱榜");
// 設置 TabLayout 模式
tab_FindFragment_title.setTabMode(TabLayout.MODE_FIXED);
// 為 TabLayout 添加 tab 名稱
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(0)));
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(1)));
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(2)));
tab_FindFragment_title.addTab(tab_FindFragment_title.newTab().setText(list_title.get(3)));
fAdapter = new Find_tab_Adapter(getActivity().getSupportFragmentManager(), list_fragment, list_title);
// 為 ViewPager 加載 adapter
vp_FindFragment_pager.setAdapter(fAdapter);
// 讓 TabLayout 與 ViewPager 關聯起來
tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
}
}
效果圖如下(這裡暫時沒做成動態效果):

以上就是本文的全部內容,希望對大家進行 Android 程序設計有所幫助。
Android 拍照截圖實作:為什麼拍照一定要走 Uri 而不能用 Bitmap
本文示範如何拍照後進行裁剪。先看效果: 拍照截圖有個先天限制:現在手機鏡頭动辄數百萬畫素,拍出的原始圖非常大。若沿用相冊那套「小圖用 Bitmap 回傳」的做法,極易
Android 6.0 執行期權限(Runtime Permission)機制源碼剖析
Android 升級到 6.0 之後,為了改善長久以來為人詬病的安全形象,在原本「安裝時一次授予」的基礎上,對其中一部分危險權限改採動態授予的方式,類似各家手機安全助手提
Android 開發學習路線與入門建議
Android 是開源行動作業系統,廣泛應用於手機、平板與其他裝置。學習時,建議優先閱讀官方 API 文件,並實際動手完成範例。學習心態不要只看教學複製貼上,遇到問題先獨
Android 使用 ZXing 生成二維碼(支持添加中心 Logo)
ZXing 是 Google 開源的一個經典庫,既能生成二維碼,也能掃描二維碼。本文聚焦第一部分——生成二維碼並支持在中心嵌入 Logo。先上效果圖:ZXing 相關文件