android toast几种使用方法 (转)
生活随笔
收集整理的這篇文章主要介紹了
android toast几种使用方法 (转)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
toast經(jīng)常會用到,今天做個總結(jié),特別是自定義toast的布局,值得一看。
一.默認(rèn)展示
二.自定義顯示位置
三、帶圖片的
四、完全自定義顯示方式
1.?toast.xml布局
<ImageViewandroid:id="@+id/image"android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> toast.xml2.代碼
public class ToastActivity extends Activity {private Button bt; private ImageView image; private TextView title, content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showToast(); } }); } private void showToast() { LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.toast, null); image = (ImageView) view.findViewById(R.id.image); title = (TextView) view.findViewById(R.id.title); content = (TextView) view.findViewById(R.id.content); image.setBackgroundResource(R.drawable.ic_launcher); title.setText("自定義toast"); content.setText("hello,self toast"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(view); toast.show(); } }?
五、其他線程通過Handler的調(diào)用
//調(diào)用方法1 //Thread th=new Thread(this); //th.start(); //調(diào)用方法2 handler.post(new Runnable() { @Override public void run() { showToast(); } }); Java代碼 public void showToast(){ Toast toast=Toast.makeText(getApplicationContext(), "Toast在其他線程中調(diào)用顯示", Toast.LENGTH_SHORT); toast.show(); } Java代碼 Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { int what=msg.what; switch (what) { case 1: showToast(); break; default: break; } super.handleMessage(msg); } }; Java代碼 @Override public void run() { handler.sendEmptyMessage(1); }轉(zhuǎn)載于:https://www.cnblogs.com/jianmang/articles/4890888.html
總結(jié)
以上是生活随笔為你收集整理的android toast几种使用方法 (转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 由数学归纳法想到的
- 下一篇: 利用lodop打印控件轻松实现批量打印