snackbar_Android Snackbar示例

snackbar_Android Snackbar示例_第1张图片

snackbar

In this tutorial you will learn about android snackbar example.

在本教程中,您将学习有关android快餐栏示例的信息。

Snackbar is a new UI widget introduced in material design. It is an advance version or we can say replacement of Toast widget. Snackbar is used to display a short message at the bottom of screen. The message can be dismissed by swiping it.

Snackbar是材料设计中引入的新UI窗口小部件。 它是一个高级版本,或者可以说是Toast小部件的替换。 小吃栏用于在屏幕底部显示一条短信。 可以通过轻扫来消除该消息。

Also Read: Android Custom Toast Example

另请参阅: Android自定义吐司示例

吐司vs小吃吧 (Toast vs Snackbar)

  • Toast can be placed anywhere on the screen while Snackbar can be placed at bottom only.

    吐司可以放在屏幕上的任何位置,而小吃店只能放在底部。

  • Toast can’t have a button while Snackbar can have at most one action button.

    吐司没有按钮,而Snackbar最多只能有一个操作按钮。

  • Toast message can’t be dismissed before its time period. Snackbar message can be dismissed by just swiping it.

    吐司消息不能在其时间段之前被关闭。 只需轻扫即可删除Snackbar消息。

简单的小吃店 (Simple Snackbar)

We can make a Snackbar by writing following line of code:

我们可以通过编写以下代码行来制作Snackbar:

Snackbar.make(coordinatorLayout, "Simple Snackbar", Snackbar.LENGTH_LONG).show();

The make() method takes 3 arguments.

make()方法采用3个参数。

First Argument: It is the root layout for activity. Here we have used CoordinatorLayout because it gives some extra functionality to Snackbar like if we have used floating button then when Snackbar is displayed the floating button automatically goes up to provide space for Snackbar. By using CoordinatorLayout we can also dismiss the Snackbar by swiping.

第一个参数:这是活动的根目录布局。 这里我们使用了CoordinatorLayout,因为它为Snackbar提供了一些额外的功能,例如,如果我们使用了浮动按钮,那么当显示Snackbar时,浮动按钮会自动向上移动,为Snackbar提供空间。 通过使用CoordinatorLayout,我们还可以通过滑动来关闭Snackbar。

Second Argument: It is the message that you want to display in Snackbar.

第二个参数:这是您要在Snackbar中显示的消息。

Third Argument: Time period for which Snackbar should be displayed.

第三个参数:应显示Snackbar的时间段。

  • Snackbar.LENGTH_SHORT: Display for short time period

    Snackbar.LENGTH_SHORT:显示时间短

  • Snackbar.LENGTH_LONG: Display for long time period

    Snackbar.LENGTH_LONG:长时间显示

  • Snackbar.LENGTH_INDEFINITE: Displayed until you close it.

    Snackbar.LENGTH_INDEFINITE:显示直到关闭它。

带操作按钮的小吃店 (Snackbar with Action Button)

You can also add an action button in Snackbar by using setAction() method. It can be done in following way.

您也可以使用setAction()方法在Snackbar中添加一个动作按钮。 可以通过以下方式完成。

Snackbar.make(coordinatorLayout,"Snackbar with Action",Snackbar.LENGTH_LONG).setAction ("OK", new View.OnClickListener() {
	@Override
	public void onClick(View v) {
        	Snackbar.make(coordinatorLayout,"You clicked on action button",Snackbar.LENGTH_SHORT).show();
	}
}).show();

You can’t add more than one button.

您不能添加多个按钮。

自定义小吃店 (Customizing Snackbar)

We can change color of text of message and action button in following way.

我们可以通过以下方式更改消息和操作按钮的文本颜色。

//set color of action button text
snackbar.setActionTextColor(Color.YELLOW);
 
//set color of snackbar text
TextView view = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
view.setTextColor(Color.GREEN);

Android Snackbar示例 (Android Snackbar Example)

Below example shows how to make different types of Snackbar in android.

下面的示例显示了如何在android中制作不同类型的Snackbar。

Create a new project with package name com.snackbarexample.

使用包名称com.snackbarexample创建一个新项目。

Note: Make sure you add dependency for android design support library in build.gradle (Module:app) file. Just add following line under dependency section and sync the project.

注意:请确保在build.gradle(Module:app)文件中添加了对android设计支持库的依赖。 只需在依赖项部分下添加以下行并同步项目即可。

compile 'com.android.support:design:23.4.0'

The library version may vary depending upon your SDK version.

库版本可能会有所不同,具体取决于您的SDK版本。

Now add following code in respective files.

现在,在相应文件中添加以下代码。

activity_main.xml

activity_main.xml



 
    
 
    
 

content_main.xml

content_main.xml



 
    

MainActivity.java

MainActivity.java

package com.snackbarexample;
 
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class MainActivity extends Activity {
    Button btn1, btn2, btn3;
    CoordinatorLayout coordinatorLayout;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
 
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
 
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(coordinatorLayout,"Simple Snackbar",Snackbar.LENGTH_LONG).show();
            }
        });
 
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(coordinatorLayout,"Snackbar with Action",Snackbar.LENGTH_LONG).setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Snackbar.make(coordinatorLayout,"You clicked on action button",Snackbar.LENGTH_SHORT).show();
                    }
                }).show();
            }
        });
 
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(coordinatorLayout,"Custom Snackbar",Snackbar.LENGTH_LONG).setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Snackbar.make(coordinatorLayout,"You clicked on action button",Snackbar.LENGTH_SHORT).show();
                    }
                });
 
                //set color of action button text
                snackbar.setActionTextColor(Color.YELLOW);
 
                //set color of snackbar text
                TextView view = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
                view.setTextColor(Color.GREEN);
 
                snackbar.show();
            }
        });
    }
}

Now save and run your project.

现在保存并运行您的项目。

Screenshot

屏幕截图

snackbar_Android Snackbar示例_第2张图片

Comment below if you are facing any problem related to above android snackbar example.

如果您遇到与上述android快餐栏示例相关的任何问题,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/08/android-snackbar-example.html

snackbar

你可能感兴趣的:(python,android,java,移动开发,android,studio)