Tab相關控件API中的介紹翻譯比較:TabLayout,FragmentTabHost,AppBarLayout

第一組:TabLayout和TabItem

TabLayout

類public class TabLayout
繼承自HorizontalScrollView
包中的位置:android.support.design.widget.TabLayout

TabLayout 提供一個顯示tabs的水平layout .
通過 TabLayout.Tab 實例添加顯示的tabs成員.可以通過 newTab().創建tabs. 在這兒你可以通過分別調用 setText(int) 和 setIcon(int) 改變tab的標識和圖標. 顯示這個 tab,你需要通過調用 addTab(Tab) 方法添加它到這個 layout . 比如:

TabLayout tabLayout = ...;
 tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
 tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
 tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));

你應該通過 setOnTabSelectedListener(OnTabSelectedListener) 設置一個監聽器,在tabs的選中狀態發生改變後收到通知.
你也可以通過使用 TabItem 向你佈局裏的TabLayout中添加items. 比如:

<android.support.design.widget.TabLayout
         android:layout_height="wrap_content"
         android:layout_width="match_parent">

     <android.support.design.widget.TabItem
             android:text="@string/tab_text"/>

     <android.support.design.widget.TabItem
             android:icon="@drawable/ic_android"/>

 </android.support.design.widget.TabLayout>

ViewPager integration(結合)

如果你讓它和 ViewPager 一塊使用,可以通過調用 setupWithViewPager(ViewPager) 把它們綁定在一塊兒. PagerAdapter 的page titles會自動填充到這個layout的tabs中.

這個 view 也支持作爲一個ViewPager’s decor的一部分使用,並且可以在layout佈局文件中直接添加到 ViewPager中 :

 <android.support.v4.view.ViewPager
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <android.support.design.widget.TabLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="top" />

 </android.support.v4.view.ViewPager>

參閱:Tabs

TabItem

類public final class TabItem
繼承自View
包中的位置:android.support.design.widget.TabItem

TabItem是一個特殊的 ‘view’,讓你在一個layout中爲 TabLayout 聲明tab items . 事實上,並不是把這個view 添加進 TabLayout, 恰當的說相當於一個框架,讓你在裏面爲 a tab items設置其文字,圖標或者自定義的 layout.去看 TabLayout可以獲得使用它的更多信息.

請看:TabLayout

第二組:TabHost和TabWidget和FragmentTabHost

TabHost

類public class TabHost
繼承自 FrameLayout 實現了ViewTreeObserver.OnTouchModeChangeListener

在包中的位置:android.widget.TabHost
已知直接子類:FragmentTabHost
一個選項卡 window view的容器. 這個對象支撐兩個子對象: 用戶點擊選中指定的tab的一組tab labels,和顯示此頁內容的一個FrameLayout 對象.使用這個容器對象時其單個元素通常也是受控制, 而不是在分別子元素上設置屬性值.

TabWidget

類public class TabWidget
繼承自 LinearLayout 實現了 View.OnFocusChangeListener
包中的位置:android.widget.TabWidget

顯示一列( a list of )在父控件的tab集合中代表每一頁的tab labels.
這個控件的容器對象是TabHost. 當用戶選中一個 tab, 這個對象向父容器TabHost發送一個信號, 告訴他轉換顯示的頁面.一般你不需要直接在這個對象上調用方法. 它的容器TabHost 被用來添加 labels, 添加回調handler, 並且管理回調. 你可以調用這個對象迭代the list of tabs, 或者調整 the tab list的佈局, 但大多數的方法應該在容器TabHost 對象中調用.

FragmentTabHost

類public class FragmentTabHost
繼承自 TabHost 實現了TabHost.OnTabChangeListener
包中的位置:android.support.v4.app.FragmentTabHost

特殊的TabHost,可以讓它的tab的內容對象爲 Fragment . 當把它放進視圖層裏,加載視圖之後必須調用setup(Context, FragmentManager, int) 來完成 tab host的初始化.
這裏有個在Activity裏使用 FragmentTabHost 的示例:

import com.example.android.supportv4.R; 

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;

/** 
 * This demonstrates how you can implement switching between the tabs of a 
 * TabHost through fragments, using FragmentTabHost. 
 */ 
public class FragmentTabs extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_tabs);
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                FragmentStackSupport.CountingFragment.class, null); 
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                LoaderCursorSupport.CursorLoaderListFragment.class, null); 
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                LoaderCustomSupport.AppListFragment.class, null); 
        mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                LoaderThrottleSupport.ThrottledLoaderListFragment.class, null); 
    } 
} 

也可以通過 fragment 嵌套fragment中使用它 :

import com.example.android.supportv4.R; 

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentTabsFragmentSupport extends Fragment {
    private FragmentTabHost mTabHost;

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);

        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                FragmentStackSupport.CountingFragment.class, null); 
        mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                LoaderCursorSupport.CursorLoaderListFragment.class, null); 
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                LoaderCustomSupport.AppListFragment.class, null); 
        mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                LoaderThrottleSupport.ThrottledLoaderListFragment.class, null); 

        return mTabHost;
    } 

    @Override 
    public void onDestroyView() { 
        super.onDestroyView(); 
        mTabHost = null;
    } 
} 

第三組:AppBarLayout和CoordinatorLayout

AppBarLayout

類public class AppBarLayout
繼承自 LinearLayout
包中的位置:android.support.design.widget.AppBarLayout

AppBarLayout是一個 LinearLayout ,實現了material designs app bar 概念的很多特性,也叫做滾動手勢(scrolling gestures).

Children子控件應該通過 setScrollFlags(int) 和相關聯的佈局文件 xml 屬性:app:layout_scrollFlags提供需要的滾動行爲.

這個view很大程度地作爲一個直接子控件依靠於 CoordinatorLayout. 如果你使用 AppBarLayout 在一個另外的 ViewGroup, 它的大多數功能就會失效 .

要知道何時滾動AppBarLayout 還需要一個分離的滾動搭檔. 通過行爲類AppBarLayout.ScrollingViewBehavior 綁定, 也就是說你需要把你的滾動的view的動作設置爲AppBarLayout.ScrollingViewBehavior的一個實例.一個包含類的全部字符串資源文件是可用的.


 <android.support.design.widget.CoordinatorLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

     <android.support.v4.widget.NestedScrollView
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:layout_behavior="@string/appbar_scrolling_view_behavior">

         <!-- Your scrolling content -->

     </android.support.v4.widget.NestedScrollView>

     <android.support.design.widget.AppBarLayout
             android:layout_height="wrap_content"
             android:layout_width="match_parent">

         <android.support.v7.widget.Toolbar
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

         <android.support.design.widget.TabLayout
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

     </android.support.design.widget.AppBarLayout>

 </android.support.design.widget.CoordinatorLayout>

See also:

http://www.google.com/design/spec/layout/structure.html#structure-app-bar

CoordinatorLayout

類public class CoordinatorLayout
繼承自 ViewGroup 實現了 NestedScrollingParent
包中的位置:android.support.design.widget.CoordinatorLayout

CoordinatorLayout是一個強有力的 FrameLayout.
CoordinatorLayout旨在用於兩個情景:

  1. 列表內容作爲一個指定與一個或多個子視圖相互作用的容器
    通過爲 CoordinatorLayout的子視圖指定行爲,你可以提供多種不同的相互影響
  2. 方式在單親(single parent)內 並且也可以影響同級別的子視圖. 當作爲一個子view使用的時候View classes可以指定一個默認的行爲 使用 DefaultBehavior註解.

Behaviors 可以用來實現各種相互作用,附加的佈局範圍修改通過滑動抽屜和可以滑動去除元素的畫板,和當移動和動畫時按鈕堅持到另一個元素.

CoordinatorLayout的子控件可能需要 anchor. 這個 view id 必須和CoordinatorLayout的任何一個 arbitrary descendant對應 , 但是它也可能不是 anchored child itself or a descendant of the anchored child. This can be used to place floating views relative to other arbitrary content panes.

第四組:Toolbar和CollapsingToolbarLayout

Toolbar

類public class Toolbar
繼承自 ViewGroup
包中的位置:android.support.v7.widget.Toolbar

應用內容內的可以使用的標準toolbar .
A Toolbar是應用佈局使用的 action bars 的一個概括. While an action bar is traditionally part of an Activity’s opaque window decor controlled by the framework, a Toolbar可以放在視圖層次結構的任意嵌套位置.應用可以使用 setSupportActionBar() 方法選擇指定Toolbar作爲一個Activity的action bar.

Toolbar比 ActionBar支持更多關注的特性.從開始到結束, a toolbar可以含有以下元素的組合:

  1. 導航按鈕. 可以是一個向上的箭頭, 導航按鈕切換,閉合,旋轉,done或者app選擇的文字 . 在Toolbar內的這個按鈕應該一直被用來通向其他的導航目的地,和其所指的內容或者其它方式離開的 Toolbar指向的環境。導航按鈕在 Toolbar內最低高度垂直對齊, 如果設置.
    圖片標誌. 可以延伸到 bar的高度並且可以任意寬.
  2. 標題和副標題. The title should be a signpost for the Toolbar’s current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
  3. 一個或多個view. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view’s Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
  4. 操作菜單An action menu. The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions. Action buttons are vertically aligned within the Toolbar’s minimum height, if set.

目前,Android UIs developers 開發者應該學習視覺上的配色,使 toolbars的顏色主題和應用的圖標視覺一致. 圖片加文字的用法在API21和更新的版本上標準佈局是不鼓勵的。

CollapsingToolbarLayout

類public class CollapsingToolbarLayout
繼承 FrameLayout
包中的位置:android.support.design.widget.CollapsingToolbarLayout

CollapsingToolbarLayout是 Toolbar 的一個包裝,實現了摺疊的 app bar.它是被設計作爲 AppBarLayout的一個直接子view使用的。 CollapsingToolbarLayout 有以下這些特性:

  1. Collapsing title摺疊標題
    A title which is larger when the layout is fully visible but collapses and becomes smaller as the layout is scrolled off screen. You can set the title to display via setTitle(CharSequence). The title appearance can be tweaked via the collapsedTextAppearance and expandedTextAppearance attributes.
  2. Content scrim
    A full-bleed scrim which is show or hidden when the scroll position has hit a certain threshold. You can change this via setContentScrim(Drawable).
  3. Status bar scrim
    A scrim which is show or hidden behind the status bar when the scroll position has hit a certain threshold. You can change this viasetStatusBarScrim(Drawable). This only works on LOLLIPOP devices when we set to fit system windows.
  4. Parallax scrolling children視差滾動的,,
    Child views can opt to be scrolled within this layout in a parallax fashion. See COLLAPSE_MODE_PARALLAX and setParallaxMultiplier(float).
  5. Pinned position children固定位置,,
    Child views can opt to be pinned in space globally. This is useful when implementing a collapsing as it allows the Toolbar to be fixed in place even though this layout is moving. See COLLAPSE_MODE_PIN.

Do not manually add views to the Toolbar at run time. We will add a ‘dummy view’ to the Toolbar which allows us to work out the available space for the title. This can interfere with any views which you add.

哎,不想寫了,留待以後整理修改吧,放在一起只是爲了更好的比較和記憶,進而靈活運用和自定義借鑑

發佈了38 篇原創文章 · 獲贊 71 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章