【Android-自定义控件】 漂亮的Toast


修改Toast属性,美化Toast

//创建一个Toast
Toast toast=new Toast(getApplicationContext());

//创建Toast中的文字
TextView textView=new TextView(getApplicationContext());
textView.setText("可爱的喵喵");
//文字设置颜色
textView.setTextColor(Color.WHITE);
//文字设置大小
textView.setTextSize(20);

//创建Toast中的图片
ImageView imageView=new ImageView(getApplicationContext());
imageView.setImageResource(R.mipmap.ic_launcher);

//组合文本加图片,可以设置线性布局
LinearLayout layout=new LinearLayout(getApplicationContext());
//设置LinearLayout垂直
layout.setOrientation(LinearLayout.HORIZONTAL);
//设置LinearLayout里面内容中心分布
layout.setGravity(Gravity.CENTER);
//先添加image
layout.addView(imageView);
//再添加text
layout.addView(textView);
//设置背景为圆角边框
layout.setBackground(getResources().getDrawable(R.drawable.message_bg));
//设置内边距
layout.setPadding(30, 20, 30, 20);

//把layout设置进入Toast
toast.setView(layout);
//设置Toast位置居中
toast.setGravity(Gravity.CENTER,0,0);
//设置显示时间
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();

圆角边框背景

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <corners android:radius="10dp"/>
            <solid android:color="#b94bb4dd"/>
        </shape>
    </item>
</selector>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM