国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開發(fā)實現(xiàn)自定義Toast、LayoutInflater使用其他布局示例

2019-12-12 00:16:37
字體:
供稿:網(wǎng)友

本文實例講述了Android開發(fā)實現(xiàn)自定義Toast、LayoutInflater使用其他布局。分享給大家供大家參考,具體如下:

內(nèi)容:

1.自定義樣式toast

2.再活動中添加其他布局

實現(xiàn)效果:

步驟:

一、自定義View 引用zidingyixml文件 生成一個布局對象

二、采用Toast 的addView() 方法將該對象添加到Toast對象中

三、顯示:Toast.show()

具體實現(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 炸了!!");    //建立提示消息對象    toast = new Toast(this);    toast.setView(insideView);  }  //按鈕點擊時彈出  public void prev(View source){    toast.show();  }}

注:R.layout.cell 中的cell 就是自定義的布局文件

建立步驟 在/values文件夾下 呢哇一個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" 方法 在布局文件中直接添加了點擊事件,故MainActivity中不用手動添加onClickListener

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總

希望本文所述對大家Android程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 科尔| 四平市| 定州市| 佛山市| 安丘市| 安福县| 于都县| 读书| 砀山县| 库伦旗| 鸡东县| 定襄县| 新绛县| 丹凤县| 武宣县| 米泉市| 临沭县| 大庆市| 平阴县| 察隅县| 吴江市| 名山县| 大同市| 霞浦县| 吉安市| 镇宁| 房产| 古交市| 新营市| 云浮市| 博兴县| 苍南县| 阿拉善盟| 元朗区| 凤冈县| 绥德县| 祁连县| 清水县| 勐海县| 岑溪市| 泌阳县|