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.

哎,不想写了,留待以后整理修改吧,放在一起只是为了更好的比较和记忆,进而灵活运用和自定义借鉴

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值