Android開(kāi)發(fā)實(shí)現(xiàn)自定義Toast、LayoutInflater使用其他布局示例
本文實(shí)例講述了Android開(kāi)發(fā)實(shí)現(xiàn)自定義Toast、LayoutInflater使用其他布局。分享給大家供大家參考,具體如下:
內(nèi)容:
1.自定義樣式toast
2.再活動(dòng)中添加其他布局
實(shí)現(xiàn)效果:
步驟:
一、自定義View 引用zidingyixml文件 生成一個(gè)布局對(duì)象
二、采用Toast 的addView()
方法將該對(duì)象添加到Toast對(duì)象中
三、顯示:Toast.show()
具體實(shí)現(xiàn)方法:
public class MainActivity extends Activity { Toast toast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //應(yīng)用布局文件 View insideView = LayoutInflater.from(MainActivity.this).inflate(R.layout.cell, null); LinearLayout linearLayout = (LinearLayout) insideView.findViewById(R.id.cell); ImageView imageView = (ImageView) insideView.findViewById(R.id.image1_Toast); TextView textView = (TextView) insideView.findViewById(R.id.textToast); imageView.setImageResource(R.drawable.warming); textView.setText("你的app 炸了??!"); //建立提示消息對(duì)象 toast = new Toast(this); toast.setView(insideView); } //按鈕點(diǎn)擊時(shí)彈出 public void prev(View source){ toast.show(); } }
注:R.layout.cell 中的cell 就是自定義的布局文件
建立步驟 在/values文件夾下 呢哇一個(gè)xml文件即可,內(nèi)容如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/cell" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/image1_Toast" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/textToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15dp"/> </LinearLayout>
最后給出整體的布局文件:
<?xml version="1.0" encoding="utf-8" ?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <Button android:onClick="prev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true"/> </RelativeLayout>
注:采用了 android:onClick="prev"
方法 在布局文件中直接添加了點(diǎn)擊事件,故MainActivity中不用手動(dòng)添加onClickListener
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android RecyclerView的卡頓問(wèn)題的解決方法
本篇文章主要介紹了Android RecyclerView的卡頓問(wèn)題的解決方法,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04Android 實(shí)現(xiàn)無(wú)網(wǎng)絡(luò)傳輸文件的示例代碼
本篇文章主要介紹了Android 實(shí)現(xiàn)無(wú)網(wǎng)絡(luò)傳輸文件的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02Android中訪問(wèn)證書(shū)有問(wèn)題的SSL網(wǎng)頁(yè)的方法
在WebView里加載SSL網(wǎng)頁(yè)很正常,也沒(méi)什么難度。但如果要加載的SSL頁(yè)面的證書(shū)有問(wèn)題,比如過(guò)期、信息不正確、發(fā)行機(jī)關(guān)不被信任等,WebView就會(huì)拒絕加載該網(wǎng)頁(yè)2014-04-04Android 指紋識(shí)別詳解及實(shí)現(xiàn)方法
本文主要介紹Android 指紋識(shí)別的知識(shí),這里整理了詳細(xì)的資料和簡(jiǎn)單實(shí)現(xiàn)代碼,有開(kāi)發(fā)這部分的朋友可以參考下2016-09-09ViewPager實(shí)現(xiàn)帶引導(dǎo)小圓點(diǎn)與自動(dòng)跳轉(zhuǎn)的引導(dǎo)界面
這篇文章主要為大家詳細(xì)介紹了ViewPager實(shí)現(xiàn)帶引導(dǎo)小圓點(diǎn)與自動(dòng)跳轉(zhuǎn)的引導(dǎo)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11Android基于Aidl的跨進(jìn)程間雙向通信管理中心
這篇文章主要為大家詳細(xì)介紹了Android基于Aidl的跨進(jìn)程間雙向通信管理中心,類似于聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android實(shí)現(xiàn)簡(jiǎn)易記事本
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易記事本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12