Toast小例子
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mBtn1;
private Button mBtn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtn1= (Button) findViewById(R.id.button_toast);
mBtn1.setOnClickListener(this);
mBtn2= (Button) findViewById(R.id.button_toast1);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button_toast:
Toast toast=Toast.makeText(getApplicationContext(),"举起你的手来,我是Toast!",Toast.LENGTH_SHORT);
Spanned spanned= Html.fromHtml("<img src=''/><font color='#ff0000'>举起你的手来</font>", new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable=getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
return drawable;
}
},null);
toast.setText(spanned);
toast.setGravity(Gravity.RIGHT,0,0);
toast.show();
break;
case R.id.button_toast1:
Toast toast1=new Toast (getApplicationContext());
LayoutInflater inflater=getLayoutInflater();
View toastView=inflater.inflate(R.layout.toast_activity, null);
TextView textviewTitle= (TextView) toastView.findViewById(R.id.textview_title);
textviewTitle.setText("我是Toast大大");
toast1.setView(toastView);
toast1.setDuration(Toast.LENGTH_LONG);
toast1.show();
break;
default:
break;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:id="@+id/button_toast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toast"/>
<Button
android:id="@+id/button_toast1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button_toast"
android:layout_marginTop="20dp"
android:text="自定义Toast"/>
</RelativeLayout>
toast_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是Toast大大"
android:textSize="40sp"/>
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/heng"
/>
<TextView
android:id="@+id/textview_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="同志们辛苦了!"
android:textSize="25sp"
/>
</LinearLayout>
轻松搞笑一刻*-*