问题
I'm trying out PercentRelativeLayout from the support library, and the docs just ask me to specify an app:layout_widthPercent property. However, when I do that, Android Studio gives me a red warn saying that the layout_width is unspecified.
Other than suppressing the warning, is there a way around this?
For example:
<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>
Warning given on TextInputLayout.
回答1:
I realize this is an old question - but having just encountered this problem I want to make sure it's available for everyone :)
Adding a view to your PercentRelativeLayout
will throw a visual error in the text editor. It may fail to render on the screen even. Go ahead and run your emulator and it should resolve the visibility issue.
To resolve the error just add android:layout_width="0dp"
So long as your widthPercent
and heightPercent
are set appropriately you'll not have an issue.
Here's my example of tested code
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"/>
回答2:
By convention, both layout_width and layout_height need to be included even when they're functionally ignored by PercentRelativeLayout's app:layout_widthPercent and app:layout_aspectRatio, respectively.
In your case, by solely using app:layout_widthPercent, just set android:layout_width to any number of density-independent pixels to get rid of the warning.
For instance: android:layout_width = "0dp"
来源:https://stackoverflow.com/questions/33682512/percentrelativelayout-layout-width-missing-warning