欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android TabLayout(選項卡布局)簡單用法實例分析

 更新時間:2016年01月13日 14:41:57   作者:chenguang79  
這篇文章主要介紹了Android TabLayout(選項卡布局)簡單用法,結(jié)合實例形式簡單分析了Android選項卡布局的界面布局與功能實現(xiàn)具體相關(guān)技巧,需要的朋友可以參考下

本文實例講述了Android TabLayout(選項卡布局)簡單用法。分享給大家供大家參考,具體如下:

我們在應(yīng)用viewpager的時候,經(jīng)常會使用TabPageIndicator來與其配合。達到很漂亮的效果。但是TabPageIndicator是第三方的,而且比較老了,當然了現(xiàn)在很多大神都已經(jīng)開始自己寫TabPageIndicator來滿足自己的需求,在2015年的google大會上,google發(fā)布了新的Android Support Design庫,里面包含了幾個新的控件,其中就有一個TabLayout,它就可以完成TabPageIndicator的效果,而且還是官方的,最好的是它可以兼容到2.2以上版本,包括2.2。下面我就舉一個簡單的例子來使用它。

這里使用的 android studio進行開發(fā)的,所以引用TabLayout很簡單,只要在build.gradle中加入compile 'com.android.support:design:22.2.0'即可。

這個使用是我在仿 知乎 的時候使用。所以頁面就和知乎很像了

fragment_find.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:app="http://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的Adapter,因為這里面我每個欄目下,都會有一些列表,所以采用list<View>的方式,在里面切換layout不太適合,所以我采用了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;
/**
 * Created by cg on 2015/9/26.
 */
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這個的說法,全在標注里面了

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;
/**
 * 發(fā)現(xiàn)頁面
 */
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; //熱門推薦fragment
  private Find_hotCollectionFragment hotCollectionFragment; //熱門收藏fragment
  private Find_hotMonthFragment hotMonthFragment; //本月熱榜fragment
  private Find_hotToday hotToday; //今日熱榜fragment
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_find, container, false);
    initControls(view);
    return view;
  }
  /**
   * 初始化各控件
   * @param 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名字列表,正常情況下,我們應(yīng)該在values/arrays.xml中進行定義然后調(diào)用
    list_title = new ArrayList<>();
    list_title.add("熱門推薦");
    list_title.add("熱門收藏");
    list_title.add("本月熱榜");
    list_title.add("今日熱榜");
    //設(shè)置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);
    //tab_FindFragment_title.setViewPager(vp_FindFragment_pager);
    //TabLayout加載viewpager
    tab_FindFragment_title.setupWithViewPager(vp_FindFragment_pager);
    //tab_FindFragment_title.set
  }
}

效果圖,不太會做成動態(tài)的:

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android中ListView的幾種常見的優(yōu)化方法總結(jié)

    Android中ListView的幾種常見的優(yōu)化方法總結(jié)

    Android中的ListView應(yīng)該算是布局中幾種最常用的組件之一,本篇文章主要做了三種優(yōu)化總結(jié),有興趣的可以了解一下。
    2017-02-02
  • Android開發(fā)筆記 改變字體顏色的三種方法

    Android開發(fā)筆記 改變字體顏色的三種方法

    在TextView中添加文本時有時需要改變一些文本字體的顏色,今天學(xué)到了三種方法,拿出來分享一下
    2012-11-11
  • Android底部彈窗的實現(xiàn)示例代碼

    Android底部彈窗的實現(xiàn)示例代碼

    本篇文章主要介紹了Android底部彈窗的實現(xiàn)示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Android實現(xiàn)雙模(CDMA/GSM)手機短信監(jiān)聽的方法

    Android實現(xiàn)雙模(CDMA/GSM)手機短信監(jiān)聽的方法

    這篇文章主要介紹了Android實現(xiàn)雙模(CDMA/GSM)手機短信監(jiān)聽的方法,涉及Android短信的原理與相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • android自定義環(huán)形對比圖效果

    android自定義環(huán)形對比圖效果

    這篇文章主要為大家詳細介紹了android自定義環(huán)形對比圖,外環(huán)有類似進度條的旋轉(zhuǎn)動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Android 啟動頁白屏解決方案

    Android 啟動頁白屏解決方案

    這篇文章主要介紹了Android 啟動頁白屏的解決方案,幫助大家更好的進行Android app的開發(fā),感興趣的朋友可以了解下
    2020-12-12
  • 在Android Studio中設(shè)置Button透明度的方法詳解

    在Android Studio中設(shè)置Button透明度的方法詳解

    本文將介紹在Android Studio中如何設(shè)置Button的透明度,首先,我們將展示實現(xiàn)該功能的整個流程,并使用表格列出每個步驟,然后,我們將詳細說明每個步驟需要做什么,并提供相應(yīng)的代碼和注釋,需要的朋友可以參考下
    2023-09-09
  • Android編程簡單實現(xiàn)雷達掃描效果

    Android編程簡單實現(xiàn)雷達掃描效果

    這篇文章主要介紹了Android編程簡單實現(xiàn)雷達掃描效果,涉及Android圖形繪制及顯示的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android Studio查看Android 5.x源碼的步驟詳解

    Android Studio查看Android 5.x源碼的步驟詳解

    Google為Android開發(fā)者帶來Android Studio,用來取代Eclipse。從Android Studio出現(xiàn)起,整機開發(fā)和Android源碼閱讀和編輯一定能用上它。這篇文章小編就帶大家學(xué)習(xí)下如何使用Android Studio查看Android 5.x源碼,有需要的可以參考借鑒。
    2016-09-09
  • Android實現(xiàn)音樂播放器歌詞顯示效果

    Android實現(xiàn)音樂播放器歌詞顯示效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)音樂播放器歌詞顯示效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06

最新評論