本文共 8956 字,大约阅读时间需要 29 分钟。
snackbar
In this tutorial we’ll discuss and implement various forms of Android Snackbar widget in our application.
在本教程中,我们将在应用程序中讨论和实现各种形式的Android Snackbar小部件。
Snackbar in android is a new widget introduced with the Material Design library as a replacement of a Toast.
android中的Snackbar是随Material Design库引入的新小部件,可替代Toast。
Android Snackbar is light-weight widget and they are used to show messages in the bottom of the application with swiping enabled. Snackbar android widget may contain an optional action button.
Android Snackbar是轻巧的小部件,用于在启用滑动的情况下在应用程序底部显示消息。 Snackbar android小部件可能包含一个可选的操作按钮。
Note: Toast message and Snackbar have display length property in common.
注意 :Toast消息和Snackbar具有共同的显示长度属性。
A code snippet to display a basic android Snackbar is shown below.
下面显示了显示基本android Snackbar的代码段。
Snackbar snackbar = Snackbar .make(coordinatorLayout, "www.journaldev.com", Snackbar.LENGTH_LONG);snackbar.show();
In the above snippet make()
method accepts three parameters:
在上面的代码段中, make()
方法接受三个参数:
show()
method is used to display the Snackbar on the screen.
show()
方法用于在屏幕上显示Snackbar。
No changes in the activity_main.xml
code which contains the CoordinatorLayout.
包含CoordinatorLayout的activity_main.xml
代码没有任何更改。
The content_main.xml
consists of three buttons. One for each type of Snackbar that we’ll be discussing.
content_main.xml
由三个按钮组成。 我们将要讨论的每种小吃店都有一个。
The code snippet for Action Call Snackbar button is given below:
下面给出了“ Action Call Snackbar”按钮的代码段:
two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG) .setAction("UNDO", new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT); snackbar1.show(); } }); snackbar.show(); } });
In the above code a new onClickListener method is invoked on clicking the action button with the respective Snackbar being displayed in it.
在上面的代码中,单击动作按钮时会调用一个新的onClickListener方法,并在其中显示相应的Snackbar。
The code snippet for Custom Snackbar that’s invoked on the second button is given below:
下面给出了第二个按钮上调用的Custom Snackbar的代码段:
three.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "Try again!", Snackbar.LENGTH_LONG) .setAction("RETRY", new View.OnClickListener() { @Override public void onClick(View view) { } }); snackbar.setActionTextColor(Color.RED); View sbView = snackbar.getView(); TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); textView.setTextColor(Color.YELLOW); snackbar.show(); } });
The MainActivity.java
is given below.
MainActivity.java
在下面给出。
package com.journaldev.snackbar;import android.graphics.Color;import android.os.Bundle;import android.support.design.widget.CoordinatorLayout;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity { CoordinatorLayout coordinatorLayout; private Button one, two, three; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "FloatingActionButton is clicked", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout); View layout= findViewById(R.id.layout); one=(Button)layout.findViewById(R.id.button); two=(Button)layout.findViewById(R.id.button2); three=(Button)layout.findViewById(R.id.button3); one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "www.journaldev.com", Snackbar.LENGTH_LONG); snackbar.show(); } }); two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "Message is deleted", Snackbar.LENGTH_LONG) .setAction("UNDO", new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message is restored!", Snackbar.LENGTH_SHORT); snackbar1.show(); } }); snackbar.show(); } }); three.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "Try again!", Snackbar.LENGTH_LONG) .setAction("RETRY", new View.OnClickListener() { @Override public void onClick(View view) { } }); snackbar.setActionTextColor(Color.RED); View sbView = snackbar.getView(); TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); textView.setTextColor(Color.YELLOW); snackbar.show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
The activity_main.xml
is unchanged.
activity_main.xml
保持不变。
The output of the snackbar android app in action is shown below.
运行中的snackbar android应用程序的输出如下所示。
This brings an end to this tutorial. You can download the final Android Snackbar project from the link below.
本教程到此结束。 您可以从下面的链接下载最终的Android Snackbar项目 。
Reference:
参考:
翻译自:
snackbar
转载地址:http://yqlzd.baihongyu.com/