0
点赞
收藏
分享

微信扫一扫

Android Toast用法详解(各种自定义Toast),android性能优化和内存优化

墨香子儿 2022-03-20 阅读 29

package hb.android.hellotoast;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

import andro

Android  Toast用法详解(各种自定义Toast),android性能优化和内存优化

id.widget.Toast;

public class HelloToastActivity extends Activity {

/* Called when the activity is first created. /

Button btn_default;

Button btn_define;

Button btn_all_define;

Button btn_image_define;

Button btn_other_thread;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

initButton();

btn_all_define.setOnClickListener(new MyOnClickListerer());

btn_define.setOnClickListener(new MyOnClickListerer());

btn_other_thread.setOnClickListener(new MyOnClickListerer());

btn_image_define.setOnClickListener(new MyOnClickListerer());

btn_default.setOnClickListener(new MyOnClickListerer());

}

public void initButton() {

btn_all_define = (Button) findViewById(R.id.btn_all_define);

btn_default = (Button) findViewById(R.id.btn_default);

btn_define = (Button) findViewById(R.id.btn_define);

btn_image_define = (Button) findViewById(R.id.btn_image_define);

btn_other_thread = (Button) findViewById(R.id.btn_other_thread);

}

private class MyOnClickListerer implements OnClickListener {

Handler handler = new Handler();

@Override

public void onClick(View v) {

if (v == btn_default) {

Toast.makeText(getApplicationContext(), "这 是默认效果",

Toast.LENGTH_SHORT).show();

} else if (v == btn_define) {

Toast toast = Toast.makeText(getApplicationContext(),

"这是自定义位置", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

} else if (v == btn_image_define) {

Toast toast = Toast.makeText(getApplicationContext(), "这是带图片的",

Toast.LENGTH_SHORT);

LinearLayout toastView = (LinearLayout) toast.getView();

ImageView imageCodeProject = new ImageView(

getApplicationContext());

imageCodeProject.setImageResource(R.drawable.ic_launcher);

toastView.addView(imageCodeProject, 0);

toast.show();

} else if (v == btn_all_define) {

LayoutInflater inflater = getLayoutInflater();

View view = inflater.inflate(R.layout.custom, null);

ImageView iv = (ImageView) view.findViewById(R.id.tvImageToast);

iv.setImageResource(R.drawable.ic_launcher);

TextView title = (TextView) view

.findViewById(R.id.tvTitleToast);

title.setText("Attention");

TextView text = (TextView) view.findViewById(R.id.tvTextToast);

text.setText("完全自定义Toast");

Toast toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(view);

toast.show();

} else if (v == btn_other_thread) {

new Thread(new Runnable() {

public void run() {

System.out.println("d");

showToast();

}

}).start();

}

}

public void showToast() {

handler.post(new Runnable() {

@Override

public void run() {

Toast.makeText(getApplicationContext(), "我来自其他线程!",

Toast.LENGTH_SHORT).show();

}

});

}

}

}

custom.xml

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

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

android:id="@+id/llToast"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#ffffffff"

android:orientation="vertical" >

<TextView

最后

希望本文对你有所启发,有任何面试上的建议也欢迎留言分享给大家。

好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划,来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。

Android  Toast用法详解(各种自定义Toast),android性能优化和内存优化

好了~如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

Android  Toast用法详解(各种自定义Toast),android性能优化和内存优化

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!

举报

相关推荐

0 条评论