Bootstrap

Android Toast在指定的Display里面显示

Android Toast在指定的Display里面显示

TextView toastTextView = null;
Toast inputModeToast = null;
private LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.CENTER);


private void createInputModeToast(){
    DisplayManager displayManager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
    Display targetDisplay = displayManager.getDisplay(mDisplayID);
    Context targetContext = createDisplayContext(targetDisplay);

    toastTextView = new TextView(targetContext);
    toastTextView.setLayoutParams(layoutParams);
    toastTextView.setWidth(420);
    toastTextView.setHeight(68);
    GradientDrawable drawable = new GradientDrawable();
    drawable.setColor(0xffffffff);
    drawable.setCornerRadius(36);
    toastTextView.setBackground(drawable);
    toastTextView.setGravity(android.view.Gravity.CENTER);
    String message = mContext.getString(R.string.input_mode_message) + " " + mDBInputMode;
    toastTextView.setText(message);
    inputModeToast = Toast.makeText(targetContext, message, Toast.LENGTH_LONG);
    inputModeToast.setView(toastTextView);
    inputModeToast.setGravity(Gravity.CENTER, 0, 350);
    Log.d(TAG, "createInputModeToast, target display id=" + targetDisplay.getDisplayId());
}

runOnUiThread(()->createDBInputModeToast());

private void processDBInputModeChange(int mode){
    mDBInputMode = mode;

    if(inputModeToast != null && toastTextView != null) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String message = mContext.getString(R.string.input_mode_message) + " " + mDBInputMode;
                Log.d(TAG, "processInputModeChange message=" + message);
                toastTextView.setText(message);
                inputModeToast.show();
            }
        });
    }
}

TextView toastTextView = null; Toast inputModeToast = null; private LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, Gravity.CENTER); private void createInputModeToast(){ DisplayManager displayManager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE); Display targetDisplay = displayManager.getDisplay(mDisplayID); Context targetContext = createDisplayContext(targetDisplay); toastTextView = new TextView(targetContext); toastTextView.setLayoutParams(layoutParams); toastTextView.setWidth(420); toastTextView.setHeight(68); GradientDrawable drawable = new GradientDrawable(); drawable.setColor(0xffffffff); drawable.setCornerRadius(36); toastTextView.setBackground(drawable); toastTextView.setGravity(android.view.Gravity.CENTER); String message = mContext.getString(R.string.input_mode_message) + " " + mDBInputMode; toastTextView.setText(message); inputModeToast = Toast.makeText(targetContext, message, Toast.LENGTH_LONG); inputModeToast.setView(toastTextView); inputModeToast.setGravity(Gravity.CENTER, 0, 350); Log.d(TAG, "createInputModeToast, target display id=" + targetDisplay.getDisplayId()); } runOnUiThread(()->createDBInputModeToast()); private void processDBInputModeChange(int mode){ mDBInputMode = mode; if(inputModeToast != null && toastTextView != null) { runOnUiThread(new Runnable() { @Override public void run() { String message = mContext.getString(R.string.input_mode_message) + " " + mDBInputMode; Log.d(TAG, "processInputModeChange message=" + message); toastTextView.setText(message); inputModeToast.show(); } }); } }

;