【问题标题】:Error:(49, 39) Fraction types not allowed (at 'layout_width' with value '50%')错误:(49, 39) 不允许使用分数类型(在“layout_width”处,值为“50%”)
【发布时间】:2017-10-23 17:38:31
【问题描述】:

我正在尝试以这种方式在 android 布局文件上将宽度设置为 50%

 android:layout_width="50%"

但我收到此错误

Error:(49, 39) Fraction types not allowed (at 'layout_width' with value '50%').

如何将宽度设置为 50%?

【问题讨论】:

标签: android


【解决方案1】:

您是否使用百分比布局?如果是这样试试这个

 app:layout_widthPercent = "50%"

【讨论】:

    【解决方案2】:

    最近您可以使用 PercentFrameLayout 和 PercentRelativeLayout 来做到这一点,但由于它们在 26.0.0 中已被弃用,因此为您的 layout_width 设置百分比值的便捷方法是使用 ConstraintLayout 中引入的指南

    这是我对另一个关于如何设置百分比值https://stackoverflow.com/a/45865283/7158449的问题的回答

    【讨论】:

      【解决方案3】:

      您需要为此使用库,因为默认情况下 Android 不提供此功能。根据您的根布局,您可能需要 PercentRelativeLayoutPercentFrameLayout

      在下面查看这个帖子:

      How to use Percentage for android layout?

      编辑,感谢@Eugene

      这在 Android 8.0 及更高版本中已被弃用,您也可以使用ConstraintLayout,如此处所述。

      https://developer.android.com/reference/android/support/percent/PercentFrameLayout.html

      【讨论】:

      • PercentFrameLayout 和 PercentRelativeLayout 在 26.0.0 中被弃用
      猜你喜欢
      • 2015-03-05
      • 2016-05-17
      • 2012-08-07
      • 2017-04-03
      • 2015-01-18
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多