//依赖
//compile 'com.android.support:design:26.+'
//布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.example.chenxiansheng.SnackbarActivity">
<Button
android:id="@+id/tan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="弹出来吧"/>
<!--显示在最下面,只能让他时间到自己消失-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/te"/>
<!--只能让他滑动消失-->
<android.support.design.widget.CoordinatorLayout android:id="@+id/container" android:layout_marginTop="150dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/></LinearLayout>
public class SnackbarActivity extends AppCompatActivity {
private CoordinatorLayout viewById;
private TextView te;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_snackbar);
viewById = findViewById(R.id.container);
Button tan = findViewById(R.id.tan);
te = findViewById(R.id.te);
tan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showSnackbar();
}
});
}
private void showSnackbar() {
//在那个控件上弹出
Snackbar.make(viewById,"陈旭",Snackbar.LENGTH_LONG)
.setAction("点击",new View.OnClickListener(){
@Override
public void onClick(View view) {
Toast.makeText(SnackbarActivity.this,"你点击了,我吐司了",Toast.LENGTH_LONG).show();
}
}).setActionTextColor(ColorStateList.valueOf(0xFFFFFF66)) //设置字体颜色
.setDuration(5000).show(); //设置显示时间
}
}