<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"    tools:context=".MainActivity">    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="toastText"        android:text="Toast显示文本" />    <Button        android:id="@+id/button2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/button1"        android:onClick="toastImage"        android:text="Toast显示图片" />    <Button        android:id="@+id/button3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/button2"        android:onClick="toastTextAndImage"        android:text="Toast显示文本和图片" /></RelativeLayout>
<pre name="code" class="java">package com.ashzheng.testcodingke;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void toastText(View v) {        Toast.makeText(this, "Hellow", Toast.LENGTH_SHORT).show();    }    public void toastImage(View v) {        Toast t = new Toast(this);        ImageView iv = new ImageView(this);        iv.setImageResource(R.mipmap.puzzle_1);        t.setView(iv);        t.setDuration(Toast.LENGTH_LONG);        t.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT, 0, 0);        t.show();    }    public void toastTextAndImage(View v){        Toast t = new Toast(this);        ImageView imageView = new ImageView(this);        imageView.setImageResource(R.mipmap.puzzle_1);        TextView textView = new TextView(this);        textView.setText("显示图文");        LinearLayout linearLayout = new LinearLayout(this);        linearLayout.setOrientation(LinearLayout.VERTICAL);        linearLayout.addView(imageView);        linearLayout.addView(textView);        t.setView(linearLayout);        t.setDuration(Toast.LENGTH_SHORT);        t.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT, 0, 0);        t.show();    }}


  

更多相关文章

  1. 用android原生代码实现联网得到图片
  2. andriod中的ImageView显示的几种方式
  3. Android实现歌词滑动显示
  4. 图片保存到sd卡
  5. Android(安卓)Toast优化
  6. Android实现图片上传
  7. Android(安卓)处理图片( 图像二值化、锐化、转换格式)
  8. Android中fragment模式
  9. android.support.v7.app.AlertDialog显示异常

随机推荐

  1. android预装apk到/system/app
  2. Android(安卓)显示确认对话框
  3. MPAndroidChart项目实战(五)——MPAndroidC
  4. Android里把View切换圆角的方法
  5. android之获取信息终端
  6. android 通过eclipse混淆代码 打包 + pro
  7. Android获取sd卡上的文件目录-日记
  8. android 加载图片防止内存溢出
  9. 实现 Android(安卓)应用在开机时自启动
  10. Android——自定义拦截器实现OKHttp网络