Android 自定义Toast设置左右间距无效的问题

Android 自定义Toast设置左右间距无效的问题

前言

项目要求需要自定义一个Toast,但是无论怎么设定,字符串长度过长的时候,就会充满屏幕宽度,非常难看。我猜点进这篇博客的遇到相同的问题的,我就懒得回头做个错误的示范了,直接说解决方法。

解决

首先去看Toast的源码

    /**
     * Make a standard toast to display using the specified looper.
     * If looper is null, Looper.myLooper() is used.
     * @hide
     */
    public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
            @NonNull CharSequence text, @Duration int duration) {
        Toast result = new Toast(context, looper);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
        TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
        tv.setText(text);

        result.mNextView = v;
        result.mDuration = duration;

        return result;
    }

可以看出,啥也没干。。。
就是嵌入个布局而已,那么重点应该就在布局上了
那就去看布局内容,结果我这边加载出来一堆乱码不知道为啥

@XX.<AN]m}�����textAppearance		textColorlayout_gravityorientationid

backgroundlayout_width

layout_height

layout_weightlayout_marginHorizontallayout_marginVerticalLinearLayoutTextViewandroid**http://schemas.android.com/apk/res/android�44��������;<����
t������������������������������������������	��������������������������������������?	����
������������������������


好吧,只好去在线源代码查看了,推荐大家一个好地方
Android OS 在线源代码
在这里插入图片描述
然后直接搜索布局文件名 transient_notification
在这里插入图片描述
然后选择其中一份点击在线查看
在这里插入图片描述
就得到代码了

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/toastFrameBackground">

    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.Toast"
        android:textColor="@color/bright_foreground_dark"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"
        />

</LinearLayout>

然后复制这个进你自定义的view XML中
随便修改,都不会出现左右间距无效的问题了,只要根目录的LinearLayout没有变
提供我的修改代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/solid_toast_bg"
    android:elevation="3dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:paddingLeft="24dp"
        android:paddingTop="12dp"
        android:paddingRight="24dp"
        android:paddingBottom="12dp"
        android:shadowColor="#00000000"
        android:shadowRadius="2.75"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

</LinearLayout>

单例使用代码:

    public void showToast(CharSequence s) {
        if (null == mToast) {
            mToast = new Toast(mContext);
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.setGravity(Gravity.CENTER, 0, 0);
            View view = LayoutInflater.from(mContext).inflate(R.layout.toast_view, null);
            mToast.setView(view);
        }
        TextView txt = mToast.getView().findViewById(R.id.message);
        txt.setText(s);
        mToast.show();
    }

在这里插入图片描述

完事

具体是为啥,还没有深究,等项目完成后,有时间再来研究下。

发布了103 篇原创文章 · 获赞 31 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/sinat_38184748/article/details/101670279