日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Android学习笔记之自定义Toast

發(fā)布時(shí)間:2025/6/15 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android学习笔记之自定义Toast 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1)布局文件layout

[html]?view plaincopy
  • <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=".MainActivity"?>??
  • ??
  • ????<Button??
  • ????????android:id="@+id/button1"??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:layout_alignParentLeft="true"??
  • ????????android:layout_alignParentTop="true"??
  • ????????android:layout_marginLeft="104dp"??
  • ????????android:layout_marginTop="70dp"??
  • ????????android:text="普通的Toast"?/>??
  • ??
  • ????<Button??
  • ????????android:id="@+id/button2"??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:layout_alignRight="@+id/button1"??
  • ????????android:layout_below="@+id/button1"??
  • ????????android:layout_marginTop="50dp"??
  • ????????android:text="自定義的Toast"?/>??
  • ??
  • </RelativeLayout>??

  • (2)需要彈出的Toast布局文件

    [html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:id="@+id/toast_layout_root"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????android:background="#DAA"??
  • ????android:orientation="horizontal"??
  • ????android:padding="8dp"?>??
  • ??
  • ????<ImageView??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:layout_marginRight="8dp"??
  • ????????android:src="@drawable/in"?/>??
  • ??
  • ????<TextView??
  • ????????android:id="@+id/text"??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:layout_marginRight="8dp"?/>??
  • ??
  • </LinearLayout>??

  • (3)類的文件

    [java]?view plaincopy
  • package?com.lc.toastdialog;??
  • ??
  • import?android.app.Activity;??
  • import?android.os.Bundle;??
  • import?android.view.Gravity;??
  • import?android.view.LayoutInflater;??
  • import?android.view.Menu;??
  • import?android.view.View;??
  • import?android.view.View.OnClickListener;??
  • import?android.widget.Button;??
  • import?android.widget.TextView;??
  • import?android.widget.Toast;??
  • ??
  • public?class?MainActivity?extends?Activity?{??
  • ??
  • ????private?Button?button1;??
  • ????private?Button?button2;??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.activity_main);??
  • ????????button1?=?(Button)?this.findViewById(R.id.button1);??
  • ????????button2?=?(Button)?this.findViewById(R.id.button2);??
  • ????????/*?
  • ?????????*?普通的Toast?
  • ?????????*/??
  • ????????button1.setOnClickListener(new?OnClickListener()?{??
  • ??
  • ????????????@Override??
  • ????????????public?void?onClick(View?v)?{??
  • ????????????????Toast.makeText(MainActivity.this,?"普通的Toast",??
  • ????????????????????????Toast.LENGTH_SHORT).show();??
  • ????????????}??
  • ????????});??
  • ????????/*?
  • ?????????*?自定義的Toast?
  • ?????????*/??
  • ????????button2.setOnClickListener(new?OnClickListener()?{??
  • ??
  • ????????????@Override??
  • ????????????public?void?onClick(View?v)?{??
  • ????????????????//?找到toast這個(gè)layout??
  • ????????????????View?layout?=?LayoutInflater.from(MainActivity.this).inflate(??
  • ????????????????????????R.layout.toast,?null);??
  • ????????????????//?對(duì)toast文件中的textView設(shè)置值??
  • ????????????????TextView?textView?=?(TextView)?layout.findViewById(R.id.text);??
  • ????????????????textView.setText("hello?world!");??
  • ??
  • ????????????????Toast?toast?=?new?Toast(MainActivity.this);??
  • ????????????????toast.setGravity(Gravity.CENTER_VERTICAL,?0,?0);??
  • ????????????????toast.setDuration(Toast.LENGTH_SHORT);??
  • ????????????????toast.setView(layout);??
  • ????????????????toast.show();??
  • ????????????}??
  • ????????});??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onCreateOptionsMenu(Menu?menu)?{??
  • ????????getMenuInflater().inflate(R.menu.main,?menu);??
  • ????????return?true;??
  • ????}??
  • ??
  • }??

  • 總結(jié)

    以上是生活随笔為你收集整理的Android学习笔记之自定义Toast的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。