xml文件:
main1:
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.toastdemo.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" > <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示带有图片的toast" android:textSize="20sp"/> <Button android:id="@+id/btn_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义的Toast" android:textSize="20sp"/> </LinearLayout> </RelativeLayout>
main2.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" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义内容"/> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> </LinearLayout>
源代码:
package com.example.toastdemo; import android.app.Activity; import android.os.Bundle; import android.text.Layout; import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; public class MainActivity extends Activity { private Button btn_1; private Button btn_2; @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); /** * 显示带有图片的Toast */ btn_1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast toast = Toast.makeText(MainActivity.this, "show Toast with the picture", Toast.LENGTH_SHORT); LinearLayout toastLayout = (LinearLayout) toast.getView(); ImageView image = new ImageView(MainActivity.this); image.setImageResource(R.drawable.ic_launcher); toastLayout.addView(image,0);//设置图片于文字上 //设置toast显示的位置,其中X方向偏移量左-右+,y方向的偏移量上-下+ toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } }); /** * 显示自定义的Toast */ btn_2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast toast = new Toast(MainActivity.this); LayoutInflater layout_inflate = LayoutInflater.from(MainActivity.this); View inflater = layout_inflate.inflate(R.layout.main2, null); toast.setView(inflater); toast.show(); } }); } }
时间: 2024-10-09 11:58:00