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屬性,在后面的代碼中我們需要使用到。在程序中,我們可以通過(guò)如下代碼創(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)行效果:
囧神的世界你不懂,蟲(chóng)哥的生活你沒(méi)有,只有程序猿的世界大家才知道。程序猿們,為了自己的精彩世界奮斗吧,努力吧!加油……
使用自定義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屬性,在后面的代碼中我們需要使用到。在程序中,我們可以通過(guò)如下代碼創(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)行效果:
囧神的世界你不懂,蟲(chóng)哥的生活你沒(méi)有,只有程序猿的世界大家才知道。程序猿們,為了自己的精彩世界奮斗吧,努力吧!加油……
相關(guān)文章
android工程下不能運(yùn)行java main程序的解決方法
這篇文章主要介紹了android工程下不能運(yùn)行java main程序的解決方法,需要的朋友可以參考下2014-05-05
Android開(kāi)發(fā)之串口編程原理和實(shí)現(xiàn)方式
提到串口編程,就不得不提到JNI,不得不提到JavaAPI中的文件描述符類(lèi):FileDescriptor;下面我分別對(duì)JNI、FileDescriptor以及串口的一些知識(shí)點(diǎn)和實(shí)現(xiàn)的源碼進(jìn)行分析說(shuō)明,感興趣的朋友可以了解下2013-01-01
Android RecyclerView滾動(dòng)定位
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView滾動(dòng)定位的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
淺談Android Studio如何Debug對(duì)應(yīng)so文件C/C++代碼
本篇文章主要介紹了淺談Android Studio如何Debug對(duì)應(yīng)so文件C/C++代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Android開(kāi)發(fā)之Wifi基礎(chǔ)教程
這篇文章主要介紹了Android開(kāi)發(fā)Wifi基礎(chǔ)教程,實(shí)例分析了Wifi的各種常見(jiàn)基本技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02

