android – PercentRelativeLayout – layout_width缺少警告

前端之家收集整理的这篇文章主要介绍了android – PercentRelativeLayout – layout_width缺少警告前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在从支持库中尝试PercentRelativeLayout,而 the docs只是要求我指定一个app:layout_widthPercent属性.但是,当我这样做时,Android Studio会给我一个红色警告,说layout_width未指定.

除了抑制警告之外,还有办法解决这个问题吗?

例如:

  1. <android.support.percent.PercentRelativeLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content">
  4.  
  5. <android.support.design.widget.TextInputLayout
  6. android:id="@+id/expiry_month_wrapper"
  7. app:layout_widthPercent="25%"
  8. android:layout_height="wrap_content">
  9. <EditText
  10. android:id="@+id/expiry_month_editText"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:maxLength="2"
  14. android:hint="@string/month"
  15. android:focusable="true"
  16. android:inputType="number"/>
  17. </android.support.design.widget.TextInputLayout>
  18.  
  19. <!-- more TextInputLayout fields -->
  20.  
  21. </android.support.percent.PercentRelativeLayout>

TextInputLayout上给出的警告.

解决方法

我意识到这是一个老问题 – 但刚刚遇到这个问题我想确保它适用于所有人:)

将视图添加到PercentRelativeLayout将在文本编辑器中引发可视错误.它甚至可能无法在屏幕上呈现.继续运行您的模拟器,它应解决可见性问题.

解决错误,只需添加android:layout_width =“0dp”只要你的widthPercent和heightPercent设置得恰当,你就不会有问题.

这是我测试代码的例子

  1. android:text="Label label"
  2. android:id="@+id/btn1"
  3. android:layout_height="wrap_content"
  4. android:layout_width="0dp"
  5. android:layout_alignParentTop="true"
  6. app:layout_widthPercent="50%"
  7. app:layout_heightPercent="30%"
  8. android:textSize="17sp"/>
  9.  
  10. <Button
  11. android:text="Other other other"
  12. android:id="@+id/btn2"
  13. android:layout_height="wrap_content"
  14. android:layout_width="0dp"
  15. android:layout_toRightOf="@id/btn1"
  16. app:layout_widthPercent="50%"
  17. app:layout_heightPercent="30%"
  18. android:textSize="17sp"/>

猜你在找的Android相关文章