Android Toast customization

1. First of all, of course, it is necessary to find the code of Toast:

Old rules, under framework/base

find -name Toast.java

That's it.

2. Find the place where the Toast interface is displayed:

It’s really easy to find, just search for the inflate function and you’ll find it

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

        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;
    }

It's in our very commonly used makeText.

3. The layout file is it:

Same for com.android.internal.R.layout.transient_notification
, framework/base/core/res/res, just find -name. Then you can change it however you want.
However, it should be noted that when adding your own picture as the background of Toast, for example, when adding a .9 picture, you need to add the resources you added in symbol.xml, and the adding method is as follows Can.

4. Additional questions

How to make toast display centered by default?

We found that the mGravity variable directly determines the Gravity method of toast, assigning it a value directly?
Wrong! ! !
rom customization, it is best not to modify the default behavior! Otherwise, the user (app code farmer) should be scolded: other mobile phones can change the position of toast display by calling setGravity, why not your mobile phone, garbage! Fortunately
, android officials have already thought of this requirement:

public Toast(Context context) {
        mContext = context;
        mTN = new TN();
        mTN.mY = context.getResources().getDimensionPixelSize(
                com.android.internal.R.dimen.toast_y_offset);
        //说的就是这行
        mTN.mGravity = context.getResources().getInteger(
                com.android.internal.R.integer.config_toastDefaultGravity);
    }
<!-- Default Gravity setting for the system Toast view. Equivalent to: Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM -->
    <integer name="config_toastDefaultGravity">0x00000051</integer>

What the hell is 0x00000051? See the annotation Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, oh! A sense of enlightenment. What is the Gravity.CENTER? Look at the definition:

public static final int CENTER = CENTER_VERTICAL|CENTER_HORIZONTAL;
//尼玛!
public static final int CENTER_VERTICAL = AXIS_SPECIFIED<<AXIS_Y_SHIFT;
public static final int CENTER_HORIZONTAL = AXIS_SPECIFIED<<AXIS_X_SHIFT;

I am lazy, write a demo, under the breakpoint, the value of Gravity.CENTER int is equal to 17, that is 0x00000011,
fill it in config.xml, prepare to compile framework-res.apk, and throw it in.

Call it a day! Go pee pee, compilation is really slow.

After 30 minutes, Nima, why is it invalid?
mtk garbage!
There must be a problem with the compiler!
No, make it all up!
Or honestly hit the log! It is found that the default gravity has indeed been changed to 17, ten thousand Cao Nima.

Before I decided to switch careers, I found an example of an app coder calling setGravity:

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

Nima, I got it:

mTN.mY = context.getResources().getDimensionPixelSize(
                com.android.internal.R.dimen.toast_y_offset);

There is also a default value that I didn't notice.

<dimen name="toast_y_offset">64dip</dimen>

Summary:
1. When something goes wrong, 99% of it is your own fault. Don't rely on the machine, platform, or others.
2. In addition to being bold, you have to be careful.

Guess you like

Origin blog.csdn.net/bberdong/article/details/71082977