展会信息港展会大全

Android自定义Toast
来源:互联网   发布日期:2015-10-03 11:20:06   浏览:1798次  

导读:mainActivity如下:嬀樀愀瘀愀崀瀀愀挀欀愀最攀 cn.c; 椀洀瀀漀爀琀 android.app.Activity; 椀洀瀀漀爀琀 android.os.Bu...

mainActivity如下:

[java]

package cn.c;

import android.app.Activity;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

//自定义Toast

public class MainActivity extends Activity {

private Button mButton;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mButton=(Button) findViewById(R.id.button);

mButton.setOnClickListener(new ClickListenerImpl());

}

private class ClickListenerImpl implements OnClickListener{

public void onClick(View v) {

Toast toast=Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT);

View toastView=

LayoutInflater.from(MainActivity.this).inflate(R.layout.toast, null);

toast.setView(toastView);

toast.show();

}

}

}

main.xml如下:

[html]

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<Button

android:id="@+id/button"

android:layout_width="150dip"

android:layout_height="50dip"

android:text="弹出Toast"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

/>

</RelativeLayout>

toast.xml如下:

[html]

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:orientation="horizontal" >

<ImageView

android:id="@+id/imageView"

android:layout_width="50dip"

android:layout_height="40dip"

android:src="@drawable/ic_launcher"

/>

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="40dip"

android:text="自定义的Toast"

android:gravity="center"

/>

</LinearLayout>

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港