android之自定義Toast使用方法
更新時(shí)間:2013年01月10日 09:20:02 作者:
有時(shí)我們的程序使用默認(rèn)的Toast時(shí)會(huì)和程序的整體風(fēng)格不搭配,這個(gè)時(shí)候我們就需要自定義Toast,使其與我們的程序更加融合,使用自定義Toast,首先我們需要添加一個(gè)布局文件,該布局文件的結(jié)構(gòu)和Activity使用的布局文件結(jié)構(gòu)一致,在該布局文件中我們需設(shè)計(jì)我們Toast的布局
Android系統(tǒng)默認(rèn)的Toast十分簡(jiǎn)潔,使用也非常的簡(jiǎn)單。但是有時(shí)我們的程序使用默認(rèn)的Toast時(shí)會(huì)和程序的整體風(fēng)格不搭配,這個(gè)時(shí)候我們就需要自定義Toast,使其與我們的程序更加融合。
使用自定義Toast,首先我們需要添加一個(gè)布局文件,該布局文件的結(jié)構(gòu)和Activity使用的布局文件結(jié)構(gòu)一致,在該布局文件中我們需設(shè)計(jì)我們Toast的布局,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#DAAA"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
在這個(gè)地方要注意,我們給LinearLayout添加的id屬性,在后面的代碼中我們需要使用到。在程序中,我們可以通過如下代碼創(chuàng)建我們自己的Toast:
public class MainActivity extends Activity
{
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//獲取LayoutInflater對(duì)象,該對(duì)象能把XML文件轉(zhuǎn)換為與之一直的View對(duì)象
LayoutInflater inflater = getLayoutInflater();
//根據(jù)指定的布局文件創(chuàng)建一個(gè)具有層級(jí)關(guān)系的View對(duì)象
//第二個(gè)參數(shù)為View對(duì)象的根節(jié)點(diǎn),即LinearLayout的ID
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
//查找ImageView控件
//注意是在layout中查找
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.head);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("自定義Toast演示程序");
Toast toast = new Toast(getApplicationContext());
//設(shè)置Toast的位置
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
//讓Toast顯示為我們自定義的樣子
toast.setView(layout);
toast.show();
}
});
}
}
運(yùn)行效果:
囧神的世界你不懂,蟲哥的生活你沒有,只有程序猿的世界大家才知道。程序猿們,為了自己的精彩世界奮斗吧,努力吧!加油……
使用自定義Toast,首先我們需要添加一個(gè)布局文件,該布局文件的結(jié)構(gòu)和Activity使用的布局文件結(jié)構(gòu)一致,在該布局文件中我們需設(shè)計(jì)我們Toast的布局,例如:
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#DAAA"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
在這個(gè)地方要注意,我們給LinearLayout添加的id屬性,在后面的代碼中我們需要使用到。在程序中,我們可以通過如下代碼創(chuàng)建我們自己的Toast:
復(fù)制代碼 代碼如下:
public class MainActivity extends Activity
{
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//獲取LayoutInflater對(duì)象,該對(duì)象能把XML文件轉(zhuǎn)換為與之一直的View對(duì)象
LayoutInflater inflater = getLayoutInflater();
//根據(jù)指定的布局文件創(chuàng)建一個(gè)具有層級(jí)關(guān)系的View對(duì)象
//第二個(gè)參數(shù)為View對(duì)象的根節(jié)點(diǎn),即LinearLayout的ID
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
//查找ImageView控件
//注意是在layout中查找
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.head);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("自定義Toast演示程序");
Toast toast = new Toast(getApplicationContext());
//設(shè)置Toast的位置
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
//讓Toast顯示為我們自定義的樣子
toast.setView(layout);
toast.show();
}
});
}
}
運(yùn)行效果:

囧神的世界你不懂,蟲哥的生活你沒有,只有程序猿的世界大家才知道。程序猿們,為了自己的精彩世界奮斗吧,努力吧!加油……
相關(guān)文章
android水平循環(huán)滾動(dòng)控件使用詳解
這篇文章主要為大家詳細(xì)介紹了android水平循環(huán)滾動(dòng)控件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android應(yīng)用獲取設(shè)備序列號(hào)的方法
本篇文章主要介紹了Android應(yīng)用獲取設(shè)備序列號(hào)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06android md5加密與rsa加解密實(shí)現(xiàn)代碼
本文將詳細(xì)介紹android上的MD5和RSA的加解密實(shí)現(xiàn)代碼分享,需要了解更多的朋友可以參考下2012-12-12在android開發(fā)中盡量不要使用中文路徑的問題詳解
本篇文章對(duì)在android開發(fā)中盡量不要使用中文路徑的問題進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05AndroidStudio構(gòu)建項(xiàng)目提示錯(cuò)誤信息“unable to find valid certification”的
這篇文章主要介紹了AndroidStudio構(gòu)建項(xiàng)目提示“unable to find valid certification”最新解決方案,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Android4.X讀取SIM卡短信和聯(lián)系人相關(guān)類實(shí)例分析
這篇文章主要介紹了Android 4.X讀取SIM卡短信和聯(lián)系人相關(guān)類,以實(shí)例形式分析了Android 4.X讀取SIM卡短信和聯(lián)系人的兩個(gè)相關(guān)類的功能、用法與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10