android之自定義Toast使用方法
更新時間:2013年01月10日 09:20:02 作者:
有時我們的程序使用默認(rèn)的Toast時會和程序的整體風(fēng)格不搭配,這個時候我們就需要自定義Toast,使其與我們的程序更加融合,使用自定義Toast,首先我們需要添加一個布局文件,該布局文件的結(jié)構(gòu)和Activity使用的布局文件結(jié)構(gòu)一致,在該布局文件中我們需設(shè)計我們Toast的布局
Android系統(tǒng)默認(rèn)的Toast十分簡潔,使用也非常的簡單。但是有時我們的程序使用默認(rèn)的Toast時會和程序的整體風(fēng)格不搭配,這個時候我們就需要自定義Toast,使其與我們的程序更加融合。
使用自定義Toast,首先我們需要添加一個布局文件,該布局文件的結(jié)構(gòu)和Activity使用的布局文件結(jié)構(gòu)一致,在該布局文件中我們需設(shè)計我們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>
在這個地方要注意,我們給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對象,該對象能把XML文件轉(zhuǎn)換為與之一直的View對象
LayoutInflater inflater = getLayoutInflater();
//根據(jù)指定的布局文件創(chuàng)建一個具有層級關(guān)系的View對象
//第二個參數(shù)為View對象的根節(jié)點,即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();
}
});
}
}
運行效果:
囧神的世界你不懂,蟲哥的生活你沒有,只有程序猿的世界大家才知道。程序猿們,為了自己的精彩世界奮斗吧,努力吧!加油……
使用自定義Toast,首先我們需要添加一個布局文件,該布局文件的結(jié)構(gòu)和Activity使用的布局文件結(jié)構(gòu)一致,在該布局文件中我們需設(shè)計我們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>
在這個地方要注意,我們給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對象,該對象能把XML文件轉(zhuǎn)換為與之一直的View對象
LayoutInflater inflater = getLayoutInflater();
//根據(jù)指定的布局文件創(chuàng)建一個具有層級關(guān)系的View對象
//第二個參數(shù)為View對象的根節(jié)點,即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();
}
});
}
}
運行效果:
囧神的世界你不懂,蟲哥的生活你沒有,只有程序猿的世界大家才知道。程序猿們,為了自己的精彩世界奮斗吧,努力吧!加油……
相關(guān)文章
android工程下不能運行java main程序的解決方法
這篇文章主要介紹了android工程下不能運行java main程序的解決方法,需要的朋友可以參考下2014-05-05
Android開發(fā)之串口編程原理和實現(xiàn)方式
提到串口編程,就不得不提到JNI,不得不提到JavaAPI中的文件描述符類:FileDescriptor;下面我分別對JNI、FileDescriptor以及串口的一些知識點和實現(xiàn)的源碼進行分析說明,感興趣的朋友可以了解下2013-01-01
淺談Android Studio如何Debug對應(yīng)so文件C/C++代碼
本篇文章主要介紹了淺談Android Studio如何Debug對應(yīng)so文件C/C++代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

