zoukankan      html  css  js  c++  java
  • 自定义Toast样式-两行文本居中显示

    toast可以设置自定义的view和显示位置。下面是一个简单的例子,复杂些的就是改变其布局文件就可以了。

    /**
     * @author BMR
     * @ClassName: ToastWithTwoText
     * @Description: TODO:
     * @date 2015/12/22 14:24
     */
    public class ToastWithTwoText {
        private static ToastWithTwoText toastWithTwoText;
    
        private Toast toast;
        private Context mContext;
    
        private ToastWithTwoText(Context context) {
            this.mContext = context;
        }
    
        public static ToastWithTwoText createToastConfig(Context context) {
            if (toastWithTwoText == null) {
                toastWithTwoText = new ToastWithTwoText(context);
            }
            return toastWithTwoText;
        }
    
        /**
         * 显示Toast
         *
         * @param tvStrOne
         * @param tvStrTwo
         */
    
        public void ToastShow(String tvStrOne, String tvStrTwo) {
            View layout = LayoutInflater.from(mContext).inflate(R.layout.layout_toast_with_two_text, null);
            TextView tvOne = (TextView) layout.findViewById(R.id.tv_text_one);
            TextView tvTwo = (TextView) layout.findViewById(R.id.tv_text_two);
            tvOne.setText(tvStrOne);
            tvTwo.setText(tvStrTwo);
            toast = new Toast(mContext);
            toast.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL, 0, 0);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);
            toast.show();
        }
    
        public void ToastShow(int idStrOne, int idStrTwo) {
            ToastShow(mContext.getString(idStrOne), mContext.getString(idStrTwo));
        }
    
    }
    View Code

    布局文件代码

    <?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="wrap_content"
        android:layout_gravity="center"
        android:paddingLeft="@dimen/common_16"
        android:paddingRight="@dimen/common_16"
        android:background="@drawable/bg_circle_cornor_rect"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/tv_text_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/common_16"
            android:gravity="center"
            android:textColor="@color/trans_white2"
            android:textSize="16dp"/>
    
        <TextView
            android:id="@+id/tv_text_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="@dimen/common_16"
            android:gravity="center"
            android:textColor="@color/trans_white2"
            android:textSize="16dp"/>
    
    
    </LinearLayout>
    View Code

    圆角背景

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/trans_black_87" />
        <corners android:topLeftRadius="8dp"
                 android:topRightRadius="8dp"
                 android:bottomRightRadius="8dp"
                 android:bottomLeftRadius="8dp"/>
    </shape>
    View Code

    资源字段

    <color name="trans_black_87">#DD000000</color> <!-- 87% trans -->
    <color name="trans_white2">#ccffffff</color>
    <dimen name="common_16">16dp</dimen>
  • 相关阅读:
    多进程或多线程实现并发
    linux服务器配置pyspider出现Could not run curl-config 的解决方式
    js的逆向解析
    修改linux终端命令行各字体颜色
    利用Centos服务器来搭建自己的splash,不再被安装的各种环境繁琐而担忧
    配置 Docker 加速器:适用于 Ubuntu14.04、Debian、CentOS6 、CentOS7、Fedora、Arch Linux、openSUSE Leap 42.1
    一分30秒 kali 开机显示 a start job is running for dev-disk 处理
    虚拟机Ubuntu16.04无法进入图形界面 The system is running in low-graphics mode
    vmware ubuntu硬盘空间不够用,空间扩展
    pip错误-failed to create process/fatal error in launcher
  • 原文地址:https://www.cnblogs.com/permanent2012moira/p/5066752.html
Copyright © 2011-2022 走看看