首页 > 代码库 > 安卓学习-界面-ui-Toast

安卓学习-界面-ui-Toast

Toast提示信息,直接上例子

两个按钮,第一个是普通的,第二个是自定义的

  

view1.xml 自定义视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#000"    android:padding="10dp"    android:orientation="horizontal" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/avatar_icon" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="执行成功"        android:textAppearance="?android:attr/textAppearanceLarge"        android:textColor="#fff" /></LinearLayout>
View Code

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/linearLayout1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="普通Toast1" />        <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="自定义Toast" /></LinearLayout>
View Code

MainActivity.java

public class MainActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Button btn1=(Button)findViewById(R.id.button1);        Button btn2=(Button)findViewById(R.id.button2);        btn1.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                Toast.makeText(MainActivity.this, "执行成功", Toast.LENGTH_SHORT).show();            }        });                   btn2.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                Toast toast=new Toast(MainActivity.this);                View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.view1, null);                toast.setView(view);                toast.setDuration(Toast.LENGTH_SHORT);                toast.show();            }        });           }}
View Code

 

安卓学习-界面-ui-Toast