解决自定义Toast(吐司)设置宽高无效的问题

网上搜索了各种教程,什么动态设置啊,什么获取屏幕宽高啊之类的,纯属扯淡,不仅繁琐,而且大多没有效果,自己抠了半天,发现一个最简单的解决办法,下面上代码:

先上JAVA代码:

/**
 * 描述:自定义吐司
 * 创建人:菜籽
 * 创建时间:2017/9/21 上午11:13
 * 备注:
 */

public class KonstantToast {

    public static int TIME_3500 = 3500;
    public static int TIME_2000 = 2000;
    public static int TIME_1000 = 1000;

    public static Toast makeText(Context context, CharSequence text, @BaseTransientBottomBar.Duration int duration) {
        Toast result = new Toast(context);

        LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflate.inflate(R.layout.sm_layout_toast_konstant, null);
        ((TextView) view.findViewById(R.id.sm_toast_tips)).setText(text);
        result.setDuration(duration);
        result.setView(view);

        return result;
    }
}

 

再上布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:background="@drawable/sm_style_bg_dialog"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/linear_inside"
        android:layout_width="250dp"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="15dp"
            android:src="@drawable/sm_img_payment_failed" />

        <TextView
            android:layout_width="1dp"
            android:layout_height="30dp"
            android:layout_marginLeft="12dp"
            android:background="@color/sm_btn_gray" />

        <TextView
            android:id="@+id/sm_toast_tips"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="12dp"
            android:textColor="@color/sm_sub_black"
            android:textSize="@dimen/text_size_14" />
    </LinearLayout>

</LinearLayout>

 

注意:控制Toast宽高的属性值在父类的LinearLayout中设置是无效的,必须设置在内部的id为linear_inside的控件上才能生效

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值