`
hilary3113
  • 浏览: 262482 次
  • 性别: Icon_minigender_1
  • 来自: 邯郸
社区版块
存档分类
最新评论

自定义Toast

阅读更多

Toast用于向用户显示一些帮助/提示,我在用其他软件时,Toast样式是他们自定义的样式,我也研究了下,做两个小例子,供大家参考,样式见附件。

有不足之处请指点

ToastActivity.java

package com.action;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ToastActivity extends Activity {
	private Button btn1;
	private Button btn2;
	private Toast toast;
	private View secondLayout;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		secondLayout = getLayoutInflater().inflate(R.layout.second, null);
		btn1 = (Button) findViewById(R.id.btn1);
		btn2 = (Button) findViewById(R.id.btn2);

		btn1.setOnClickListener(new BtnListener(btn1));
		btn2.setOnClickListener(new BtnListener(btn2));
	}

	class BtnListener implements OnClickListener {
		private View view;

		public BtnListener(View view) {
			this.view = view;
		}

		@Override
		public void onClick(View v) {
			if (view.equals(btn1)) {
				toast = Toast.makeText(getApplicationContext(), "自定义Toast位置",
						Toast.LENGTH_LONG);  //自定义Toast位置
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else if (view.equals(btn2)) {
				toast = new Toast(getApplicationContext());  //自定义Toast样式
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.setView(secondLayout);
				toast.show();
			}
		}
	}
}

 

 main.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
	<Button android:id="@+id/btn1" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Button" />
	<Button android:id="@+id/btn2" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="Button" />
</LinearLayout>

 

 

 

second.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal" android:layout_width="fill_parent"
	android:background="@drawable/toast_frame"
	android:gravity="center_vertical"
	android:layout_height="fill_parent">
	<ImageView android:id="@+id/img" android:layout_width="wrap_content"
		android:src="@drawable/btn_check_buttonless_on" android:layout_height="wrap_content" />
	<TextView android:id="@+id/tv" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="second" />
</LinearLayout>

 

 

 

  • 大小: 14.8 KB
分享到:
评论
1 楼 sunbird.work 2013-02-19  
niub,楼主真是太牛逼了

相关推荐

Global site tag (gtag.js) - Google Analytics