安卓之Toast

设置步骤详见讲义:39/59
package com.example.my_toast;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout linearLayout = findViewById(R.id.liner_item);
//获取局布局View
        View view = LinearLayout.inflate(MainActivity.this,R.layout.item,
                null);
        Toast toast = new Toast(MainActivity.this);
        //设置吐司的View
        toast.setView(view);
        //设置显示时间
        toast.setDuration(Toast.LENGTH_LONG);
        //设置位置
        toast.setGravity(Gravity.TOP|Gravity.CENTER,50,100);
        //显示
        toast.show();
    }
    public void onClick(View view) {

        //获取局布局View
        View view1 = LinearLayout.inflate(MainActivity.this,R.layout.item,
                null);
        Toast toast = new Toast(MainActivity.this);
        //设置吐司的View
        toast.setView(view1);
        //设置显示时间
        toast.setDuration(Toast.LENGTH_LONG);
        //设置位置
        toast.setGravity(Gravity.TOP|Gravity.CENTER,50,100);
        //显示
        toast.show();
    }
}

activity_main.xml



    
item.xml:



    
    



你可能感兴趣的:(安卓之Toast)