【问题标题】:Setting layout_width, layout_height using a style使用样式设置 layout_width、layout_height
【发布时间】:2013-06-09 10:49:38
【问题描述】:

我使用样式文件为相同的布局设置了独特的样式,

风格:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
   <!-- User form layout style -->
   <style name="UserFormLayoutRow">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:orientation">horizontal</item>
    <item name="android:paddingBottom">@dimen/padding_small</item>
   </style>
</resources >

//在布局文件中

 <LinearLayout
   style="@style/UserFormLayoutRow" >
         //contents
 </LinearLayout>

Error:(design time)

运行时出错:

06-13 05:58:14.506: E/AndroidRuntime(936): Caused by: java.lang.RuntimeException: Binary XML file line #105: You must supply a layout_width attribute.

我哪里出错了? TIA

【问题讨论】:

  • 你应该指定线性布局的宽度和高度。我猜你没有这样做
  • @Raghunandan 我在样式文件中指定了它,对吗?我不想再写同样的东西了。假设有一百个线性布局,所以我想把它放在样式文件中并从那里得到它?
  • 清单中的最低和目标 SDK 版本是多少?
  • 对我来说同样有效,你确定第 105 行具有相同的 LinearLayout 吗?
  • 您是否只是在res\values 文件夹中定义了它,而不是在values-v11values-v14 文件夹中定义了它?您也可以尝试根据this 问题在这两个文件夹中定义它们。

标签: android android-layout layout android-linearlayout


【解决方案1】:

编辑我检查了你的场景。看来 layout_width 和 layout_height 必须在 layout 下定义。如果您不想在 xml 中提及它们,请在 style.xml 中提及并将其用于您的视图。

【讨论】:

  • 你能给我一个官方链接吗
  • @RajaJawahar,我遇到了一个问题,当我使用 styles.xml 应用 layout_width 和 layout_height 时,上面提到的样式在视图中不适用。你能提供一些示例代码吗?或者我在哪里遗漏了什么?
【解决方案2】:

即使我遇到了同样的问题,我解决的方法只是在布局 xml 文件中为高度和宽度提供 wrap_content。然后在样式 xml 文件中覆盖您想要的任何高度和宽度,因此现在您无需在布局 xml 文件中再次更改该值。最后只需将样式属性添加到相应的视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2013-10-07
    • 2011-02-17
    • 2015-04-07
    • 2012-10-30
    • 2016-05-19
    • 1970-01-01
    相关资源
    最近更新 更多