Snackbar的使用流程

//依赖


//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();                                 //设置显示时间

    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值