我正在使用 Android Studio 开发 Android 应用程序。 我在 ..../src/main/res/values 中有一个样式资源 -
colors.xml
dimens.xml
strings.xml
styles.xml
我在 Styles.xml 文件中定义了几个样式,工作室抛出错误,指出样式无法在它解析的位置解析,并应用于同一 Activity 代码和同一 styles.xml 文件中的一个 LinearLayout。 我正在尝试将另一种样式应用于有效的 LinearLayout 内的 Button。
在样式中——
<style name="LoginButton">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:layout_marginLeft">25dp</item>
<item name="android:layout_marginRight">25dp</item>
<item name="android:background">@color/navy_blue</item>
<item name="android:textColor">@color/white</item>
<item name="android:text">@string/action_sign_in</item>
<item name="android:paddingLeft">50dp</item>
<item name="android:paddingRight">50dp</item>
<item name="android:layout_gravity">center</item>
</style>
在 activity_login.xml 上 -
<Button style="@style/LoginButton"
android:id="@+id/sign_in_button"
/>
android studio 显示的错误-
Rendering Problems NOTE: One or more layouts are missing the layout_width or layout_height attributes. These are required in most layouts. <Button> does not set the required layout_width attribute: Set to wrap_content, Set to match_parent <Button> does not set the required layout_height attribute: Set to wrap_content, Set to match_parent Or: Automatically add all missing attributes Couldn't resolve resource @style/LoginButton (6 similar errors not shown)
最佳答案
只需将 android:layout_width
和 android:layout_height
移动到您的按钮。
Android 并不总是从样式中读取此字段。这取决于Android需要什么参数来定位元素。
我认为this answer可能更有帮助。
关于Android @style 资源未解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177241/