【问题标题】:Why Gravity.FILL_HORIZONTAL filling only half of the screen in Wear OS?为什么 Gravity.FILL_HORIZONTAL 在 Wear OS 中只填充一半屏幕?
【发布时间】:2020-11-07 23:30:03
【问题描述】:

我使用 Gravity.FILL 和 Gravity.FILL_HORIZONTAL 来制作全屏吐司。 但它不起作用。它只水平填充了一半的屏幕。

Toast.java

Toast myToast = Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT);
myToast.setMargin(0,0);
myToast.setGravity( Gravity.FILL, 0, 0);
//myToast.setGravity( Gravity.FILL_VERTICAL | Gravity.FILL_HORIZONTAL, 0, 0);

myToast.show();

结果

【问题讨论】:

    标签: android wear-os toast android-toast android-gravity


    【解决方案1】:

    这里实际发生的是 toast 的宽度设置为包裹内容。如果添加更长的字符串,您会发现它会增长到覆盖整个屏幕。

    强制它始终填满整个屏幕的一种有点老套的方法是简单地更改 Toast 中文本字段的宽度,如下所示:

    Toast myToast = Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT);
    myToast.setGravity(Gravity.FILL_VERTICAL, 0, 0);
    
    TextView toastMessage = myToast.getView().findViewById(android.R.id.message);
    // In case the name of the TextView is different for different versions of Wear OS.
    if (toastMessage != null) {  
        toastMessage.setMinWidth(480); // Replace with the actual screen width
    }
    
    myToast.show();
    

    不漂亮。这不是面向未来的。但它有效。 :)

    解决此问题的另一种方法是为 Toast 提供您自己的视图。这将消除代码在未来意外中断的风险。缺点是您的 Toast 可能看起来与其他通用 Toast 不同(这可能会让用户感到困惑)。

    我的建议是直接使用 Toast。不要试图强迫它填满屏幕。让它以其默认大小显示在默认位置。这确保了安全和一致的体验。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 2021-01-12
      相关资源
      最近更新 更多