转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/61194122
1、默认Toast
2、自定义位置Toast
3、带图片Toast
4、完全自定义Toasts
5、外部方法Toast
代码如下:
package com.example.leixiansheng.mtoast; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_1; private Button btn_2; private Button btn_3; private Button btn_4; private Button btn_5; private final String TEXT_1 = "默认Toast"; private final String TEXT_2 = "自定义位置Toast"; private final String TEXT_3 = "带图片Toast"; private final String TEXT_4 = "完全自定义Toasts"; private EditText editText; private Toast toast; private String str; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_1 = (Button) findViewById(R.id.btn_1); btn_2 = (Button) findViewById(R.id.btn_2); btn_3 = (Button) findViewById(R.id.btn_3); btn_4 = (Button) findViewById(R.id.btn_4); btn_5 = (Button) findViewById(R.id.btn_5); editText = (EditText) findViewById(R.id.edit_text); btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); btn_5.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_1: toast.makeText(MainActivity.this, TEXT_1, Toast.LENGTH_SHORT).show(); break; case R.id.btn_2: toast = Toast.makeText(this, TEXT_2, Toast.LENGTH_SHORT); // toast = new Toast(this); // TextView textView = new TextView(this); // textView.setText(TEXT_2); // toast.setDuration(Toast.LENGTH_SHORT); // toast.setView(textView); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); break; case R.id.btn_3: toast = new Toast(this); ImageView imageView = new ImageView(this); imageView.setImageResource(R.mipmap.ic_launcher); toast.setView(imageView); toast.show(); break; case R.id.btn_4: LayoutInflater inflater = getLayoutInflater(); View view1 = inflater.inflate(R.layout.mytoast, null); TextView text = (TextView) view1.findViewById(R.id.text); ImageView imageView1 = (ImageView) view1.findViewById(R.id.image); toast = new Toast(this); toast.setGravity(Gravity.TOP, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(view1); toast.show(); break; case R.id.btn_5: MyToast myToast = new MyToast(); str = editText.getText().toString(); if (str.equals("")) { str = "请输入内容"; } myToast.showText(str); break; } } public class MyToast { public void showText(String string) { Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT).show(); } } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_1" android:text="默认Toast" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_2" android:text="自定义位置Toast" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_3" android:text="带图片Toast" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_4" android:text="完全自定义Toast" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_5" android:text="外部方法Toast" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/edit_text" android:hint="请输入内容" android:gravity="center" android:maxLines="1" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/my_toast" android:background="#111111" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/image" android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text" android:text="完全自定义Toast" android:textSize="20dp" android:textColor="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
