SnackBar 简单分析
吕福刚
8年前
<h3>介绍</h3> <p>Snackbars provide lightweight feedback about an operation. They show a brief message at the</p> <p>bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other</p> <p>elements on screen and only one can be displayed at a time.</p> <p>They automatically disappear after a timeout or after user interaction elsewhere on the screen,</p> <p>particularly after interactions that summon a new surface or activity. Snackbars can be swiped</p> <p>off screen.</p> <p>Snackbars can contain an action which is set via</p> <p>{@link #setAction(CharSequence, android.view.View.OnClickListener)}.</p> <p>To be notified when a snackbar has been shown or dismissed, you can provide a {@link Callback} via {@link #setCallback(Callback)}.</p> <p>翻译:</p> <p>Snackbar为用户的一个操作提供轻量级的反馈,他会在手机屏幕的底部或者是大设备的左下方显示一个简洁的消息。Snackbar出现在屏幕所有元素的上面,显示一小会。</p> <p>Snackbar显示一会后会自动消失,用户点击其他地方的时候也会消失,尤其,当显示一个新的界面的时候,Snackbar会滑出界面。</p> <p>Snackbar 可以通过setAction(CharSequence, android.view.View.OnClickListener) 来设置一个点击事件,同时也可以通过setCallback(Callback)来监听snackbar的显示和消失。</p> <h3>类介绍</h3> <p>Snackbar中Public属性列表</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/92fcb5bdff562f82743879bba9f00cec.png"></p> <table> <thead> <tr> <th>方法或接口</th> <th>意义</th> </tr> </thead> <tbody> <tr> <td>Callback</td> <td>抽象函数用来设置SnackBar显示和消失的回调</td> </tr> <tr> <td>make</td> <td>创建SnackBar</td> </tr> <tr> <td>setAction</td> <td>设置ActionView的响应事件</td> </tr> <tr> <td>show</td> <td>显示SnackBar</td> </tr> <tr> <td>dismiss</td> <td>收起SnackBar</td> </tr> <tr> <td>setActionTextColor</td> <td>设置ActionView的字体颜色</td> </tr> <tr> <td>setText</td> <td>设置TextView的显示内容</td> </tr> <tr> <td>setDuration</td> <td>设置SnackBar的显示时间</td> </tr> <tr> <td>getDuration</td> <td>获取SnackBar的显示时间</td> </tr> <tr> <td>getView</td> <td>获取SnackBar的View视图</td> </tr> <tr> <td>setCallback</td> <td>设置显示和消失的事件监听</td> </tr> <tr> <td>isShown</td> <td>是否已经显示</td> </tr> <tr> <td>shownOrQueued</td> <td>是否已经显示或者在队列中等待显示</td> </tr> </tbody> </table> <p>整理而言的话,感觉Snackbar就是Toast的一个升级版本,使用的方式和Toast也比较相似。</p> <p>创建Snackbar之后一定要记得调用show()方法</p> <h3>具体使用</h3> <p>–代码内容–</p> <pre> <code class="language-java">package mraz.com.snackdemo; import android.os.Bundle; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { RelativeLayout rootLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rootLayout = (RelativeLayout) findViewById(R.id.root_layout); Button shortButton = (Button) findViewById(R.id.short_button); Button longButton = (Button) findViewById(R.id.long_button); shortButton.setOnClickListener(this); longButton.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.short_button: final Snackbar shortSnackbar = Snackbar.make(rootLayout, " Short SnackBar", Snackbar.LENGTH_SHORT); shortSnackbar.setAction("Click", new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(rootLayout.getContext(), "Shoort Snackbar onClick", Toast.LENGTH_SHORT).show(); } } ); shortSnackbar.setActionTextColor(getResources().getColor(R.color.colorPrimaryDark)); shortSnackbar.show(); break; case R.id.long_button: Snackbar longSnackbar = Snackbar.make(rootLayout, " Long SnackBar", Snackbar.LENGTH_LONG); longSnackbar.setCallback(new SnackCallback()); longSnackbar.show(); break; } } class SnackCallback extends Snackbar.Callback { @Override public void onDismissed(Snackbar snackbar, int event) { super.onDismissed(snackbar, event); Toast.makeText(rootLayout.getContext(), "SnackBar onDismissed", Toast.LENGTH_SHORT).show(); } @Override public void onShown(Snackbar snackbar) { super.onShown(snackbar); Toast.makeText(rootLayout.getContext(), "SnackBar onShown", Toast.LENGTH_SHORT).show(); } } }</code></pre> <h3>实际效果</h3> <p style="text-align:center"><img src="https://simg.open-open.com/show/642169a276746672d80da9b485581245.gif"></p> <p> </p> <p>来自:http://blog.csdn.net/poorkick/article/details/51814023</p> <p> </p>