本文介绍了Android Design支持库有哪些新功能以及如何使用其Snackbar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

面向开发人员的Android M Preview于昨天发布.与往常一样,引入了许多惊人的新功能.我注意到Snackbar是其中之一.

Android M Preview for developers was released yesterday. As usual, many amazing new features are introduced. I noticed that Snackbar is one of them.

我已经阅读了有关Snackbar的文档,从中我了解到Snackbar位于Android设计支持库的库中,其绝对路径为 android.support.design.widget.Snackbar .

I have read the document about Snackbar, from which I learnt that Snackbar is in the library of Android Design Support Library, whose absolute path is android.support.design.widget.Snackbar.

文档说:

它们在超时或用户交互后自动消失 在屏幕上的其他位置,特别是在互动召唤出 新的表面或活动.小吃店可以在屏幕外滑动.

They automatically disappear after a timeout or after user interaction elsewhere on the screen, particularly after interactions that summon a new surface or activity. Snackbars can be swiped off screen.

那么,Snackbar的行为类似于Toast还是Dialog? Snackbars可以在布局文件中使用吗?我如何以编程方式使用它?

So, does Snackbar behave like a Toast or a Dialog? Can Snackbars be used in a layout file? How could I use it programmatically?

PS:

  • 任何使用Snackbar的样品都会受到赞赏.
  • Android设计支持库是一个新的支持库,可以有人给我看了这个图书馆的更多细节吗?
  • Any samples on use of Snackbar will be appreciated.
  • Android Design Support Library is a new support library, couldsomebody show me more details of this library?

推荐答案

对于Snackbar,它的作用类似于Toast,但与Toast不同. 小吃栏显示在屏幕底部,并且包含带有可选的单个操作的文本.通过在屏幕上设置动画,它们会在给定的时间长度后自动超时.此外,用户还可以在超时之前将其滑动离开,这比另一种轻量级的反馈机制烤面包要强大得多.

As for Snackbar,it acts like a Toast but is different with a Toast. Snackbars are shown on the bottom of the screen and contain text with an optional single action. They automatically time out after the given time length by animating off the screen. In addition, users can swipe them away before the timeout which is considerably more powerful than toasts, another lightweight feedback mechanism.

您可以像这样以编程方式使用它:

You can use it programmatically like this:

Snackbar snackbar = Snackbar
  .make(parentLayout, R.string.snackbar_text, Snackbar.LENGTH_LONG)
  .setAction(R.string.snackbar_action, myOnClickListener);
snackbar.setActionTextColor(Color.CYAN);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.YELLOW);//change Snackbar's background color;
TextView textView = (TextView)snackbarView .findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.BLUE);//change Snackbar's text color;
snackbar.show(); // Don’t forget to show!

请注意,在make()方法中使用视图-Snackbar会尝试找到它以确保将其锚定在其底部.

Note the use of a View in the method of make() - Snackbar will attempt to find it ensure that it is anchored to its bottom.

此外, Android设计支持库用于 Android 2.1+(API 7 +),该功能具有导航抽屉视图,浮动标签,浮动操作按钮快餐栏标签等.

What's more, Android Design Support Library is used for Android 2.1+ (API 7+), which features navigation drawer view, floating labels for editing text, floating action button, snackbar, tabs and something like that.

导航视图

导航抽屉可以成为应用程序中身份和导航的重要焦点,此处的设计一致性可以显着改变应用程序的导航方式,尤其是对于初次使用的用户. NavigationView通过提供导航抽屉所需的框架以及通过菜单资源为导航项充气的功能,使此操作变得更加容易.

The navigation drawer can be an important focal point for identity and navigation within your app and consistency in the design here can make a considerable difference in how easy your app is to navigate, particularly for first time users. NavigationView makes this easier by providing the framework you need for the navigation drawer as well as the ability to inflate your navigation items through a menu resource.

您可以像这样使用它:

<android.support.v4.widget.DrawerLayout
        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:fitsSystemWindows="true">
    <!-- your content layout -->
    <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

关于抽屉菜单,可能是:

As for the drawer menu, it could be:

<group android:checkableBehavior="single">
    <item
        android:id="@+id/navigation_item_1"
        android:checked="true"
        android:icon="@drawable/ic_android"
        android:title="@string/navigation_item_1"/>
    <item
        android:id="@+id/navigation_item_2"
        android:icon="@drawable/ic_android"
        android:title="@string/navigation_item_2"/>
</group>

或:

<item
    android:id="@+id/navigation_subheader"
    android:title="@string/navigation_subheader">
    <menu>
        <item
            android:id="@+id/navigation_sub_item_1"
            android:icon="@drawable/ic_android"
            android:title="@string/navigation_sub_item_1"/>
        <item
            android:id="@+id/navigation_sub_item_2"
            android:icon="@drawable/ic_android"
            android:title="@string/navigation_sub_item_2"/>
    </menu>
</item>

通过使用setNavigationItemSelectedListener()设置OnNavigationItemSelectedListener,您将获得对选定项目的回调.这为您提供了被单击的MenuItem,允许您处理选择事件,更改选中状态,加载新内​​容,以编程方式关闭抽屉或您可能想要的任何其他操作.

You’ll get callbacks on selected items by setting a OnNavigationItemSelectedListener using setNavigationItemSelectedListener(). This provides you with the MenuItem that was clicked, allowing you to handle selection events, changed the checked status, load new content, programmatically close the drawer, or any other actions you may want.

用于编辑文本的浮动标签

即使不起眼的EditText在材料设计上仍有改进的空间.虽然仅EditText会在键入第一个字符后隐藏提示文本,但您现在可以将其包装在TextInputLayout中,从而使提示文本成为>上方的浮动标签. ,确保用户在输入内容时不会失去上下文.除了显示提示以外,您还可以通过调用setError()在EditText下方显示错误消息.

Even the humble EditText has room to improve in material design. While an EditText alone will hide the hint text after the first character is typed, you can now wrap it in a TextInputLayout, causing the hint text to become a floating label above the EditText, ensuring that users never lose context in what they are entering. In addition to showing hints, you can also display an error message below the EditText by calling setError().

浮动操作按钮

浮动操作按钮是圆形按钮,表示界面上的主要操作.设计库的FloatingActionButton为您提供了一个一致的实现,默认情况下使用主题中的colorAccent进行着色.

A floating action button is a round button denoting a primary action on your interface. The Design library’s FloatingActionButton gives you a single consistent implementation, by default colored using the colorAccent from your theme.

随着FloatingActionButton扩展ImageView,您将使用android:src或setImageDrawable()之类的任何方法来控制FloatingActionButton中显示的图标.

As FloatingActionButton extends ImageView, you’ll use android:src or any of the methods such as setImageDrawable() to control the icon shown within the FloatingActionButton.

标签

顶级导航模式通常用于组织不同的内容分组.设计库的TabLayout既实现了固定的选项卡,其中视图的宽度在所有选项卡之间平均分配,也实现了可滚动的选项卡,其中选项卡的大小不是统一的,可以水平滚动.

top level navigation pattern is commonly used for organizing different groupings of content. The Design library’s TabLayout implements both fixed tabs, where the view’s width is divided equally between all of the tabs, as well as scrollable tabs, where the tabs are not a uniform size and can scroll horizontally.

标签可以通过编程方式添加:

Tabs can be added programmatically:

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

如果要使用ViewPager在选项卡之间进行水平分页,则可以直接从PagerAdapter’s getPageTitle()创建选项卡,然后使用setupWithViewPager()将两者连接在一起.这样可确保选项卡选择事件更新ViewPager,并且页面更改会更新所选的选项卡.

If you want to use ViewPager for horizontal paging between tabs, you can create tabs directly from your PagerAdapter’s getPageTitle() and then connect the two together using setupWithViewPager(). This ensures that tab selection events update the ViewPager and page changes update the selected tab.

CoordinatorLayout和应用栏

设计库引入了CoordinatorLayout,该布局提供了对子视图之间的触摸事件的附加控制级别,设计库中的许多组件都利用了该控件.如果尝试使用AppBarLayout,则允许Toolbar和其他视图(例如TabLayout提供的选项卡)对标有ScrollingViewBehavior的同级视图中的滚动事件做出反应.因此,您可以创建一个布局,例如:

the Design library introduces CoordinatorLayout, a layout which provides an additional level of control over touch events between child views, something which many of the components in the Design library take advantage of. If you try using an AppBarLayout allows your Toolbar and other views (such as tabs provided by TabLayout) to react to scroll events in a sibling view marked with a ScrollingViewBehavior. Therefore you can create a layout such as:

<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">

     <! -- Your Scrollable View -->
    <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
   <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>

现在,当用户滚动RecyclerView时,AppBarLayout可以通过使用儿童的滚动标志来控制其进入(在屏幕上滚动)和退出(在屏幕外滚动)的方式来响应这些事件.

Now, as the user scrolls the RecyclerView, the AppBarLayout can respond to those events by using the children’s scroll flags to control how they enter (scroll on screen) and exit (scroll off screen).

设计库,AppCompat和所有Android支持库都是重要的工具,可提供构建现代,美观的Android应用所需的构建基块,而无需从头开始构建所有内容.

The Design library, AppCompat, and all of the Android Support Library are important tools in providing the building blocks needed to build a modern, great looking Android app without building everything from scratch.

这篇关于Android Design支持库有哪些新功能以及如何使用其Snackbar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 20:22