PercentRelativeLayout - layout_width缺少警告

kos*_*kos 6 android android-support-library android-studio

我正在从支持库中尝试PercentRelativeLayout,文档只是要求我指定一个app:layout_widthPercent属性.但是,当我这样做时,Android Studio会给我一个红色警告,说layout_width未指定.

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

例如:

    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/expiry_month_wrapper"
            app:layout_widthPercent="25%"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/expiry_month_editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLength="2"
                android:hint="@string/month"
                android:focusable="true"
                android:inputType="number"/>
        </android.support.design.widget.TextInputLayout>

      <!-- more TextInputLayout fields -->

    </android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)

TextInputLayout上给出的警告.

小智 8

按照惯例,即使它们在功能上被PercentRelativeLayout的app:layout_widthPercent和app:layout_aspectRatio忽略,也需要包含layout_width和layout_height.

在您的情况下,通过单独使用app:layout_widthPercent,只需将android:layout_width设置为任意数量的与密度无关的像素,以消除警告.

例如:android:layout_width ="0dp"

  • 但要小心......我们知道"wrap_content",例如,PercentRelativeLayout具有非常特殊的含义......字段不会被完全忽略. (3认同)

BR8*_*R89 1

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

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

要解决该错误,只需添加android:layout_width="0dp"只要您的widthPercentheightPercent设置正确,就不会有问题。

这是我测试的代码示例

            android:text="Label label"
            android:id="@+id/btn1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_alignParentTop="true"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="30%"
            android:textSize="17sp"/>

        <Button
            android:text="Other other other"
            android:id="@+id/btn2"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_toRightOf="@id/btn1"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="30%"
            android:textSize="17sp"/>
Run Code Online (Sandbox Code Playgroud)