【问题标题】:Why shouldn't I set layout_width and layout_height in a style?为什么不应该在样式中设置 layout_width 和 layout_height?
【发布时间】:2013-05-11 13:47:36
【问题描述】:

如果我有很多 TextViews 作为项目标签,我想我可以将它们的所有共同点提取到一个样式中,并且只在布局中使用

<TextView style="@style/label" android:text="Foo"/>
<TextView style="@style/label" android:text="Bar"/>

风格如下:

<style name="label">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

但是当我这样做时,它在模拟器和设备上运行良好,但 Android XML 编辑器抱怨 "&lt;TextView&gt;" does not set the required layout_height attribute. 为什么它报告此警告有什么原因吗?

【问题讨论】:

    标签: android eclipse android-layout adt


    【解决方案1】:

    您是否尝试过清理项目?

    我在一些 XML 文件的宽度和长度上也使用了相同的东西。 这是一个在我的应用上工作的例子,你可以看看:

    <TextView
        android:id="@+id/viewdescription"
        android:textIsSelectable="true"
        android:layout_below="@+id/rating_bar_view"
        android:minLines="8"
        style="@style/description" />
    

    还有 XML:

    <style name="description">
       <item name="android:layout_gravity">center_horizontal</item>
       <item name="android:inputType">textMultiLine</item>
       <item name="android:layout_width">fill_parent</item>
       <item name="android:layout_height">wrap_content</item>     
       <item name="android:textSize">14sp</item>
       <item name="android:textColor">@color/Black</item>
       <item name="android:layout_marginRight">8dp</item>
       <item name="android:layout_marginLeft">8dp</item>
    

    【讨论】:

    • 它工作正常,但是当我切换到图形布局时它会在 Eclipse 中触发警告
    • 对不起,我误会了。我检查了图形布局,但没有看到任何警告。但另一方面,我也发现了关于主题 (Couldn't find theme resource categories for the current theme) 的警告,这些主题实际上在应用程序上正常工作。也许是与 Eclipse 相关的问题...
    • 您解决过这个问题吗?我也看到了这一点,但是当我运行应用程序时它崩溃了,而以前它没有。似乎 Eclipse 混淆了资源并且未能为我链接样式,但我无法修复它。清理、重新启动 eclipse 和其他一些事情并没有解决我的问题。
    • 是否 any1 找到了解决方案。最近,我们将项目从 Ant 构建迁移到基于 Gradle 的构建,应用程序崩溃了。我们已经在样式中定义了宽度和高度参数,并将它们移回布局 xml 解析。但是我们现在有大量的 XML,这是一项乏味的工作
    • @MRX 我也在使用 Gradle,并且在某些样式中我仍然有高度和宽度参数。我不明白 Gradle 与它有什么关系,我认为它与您的问题无关。
    【解决方案2】:

    老话题,但仍然:)

    我不认为为样式设置宽度和高度是一个好主意。基本上它可以编译并且通常可以工作,但是我遇到了更复杂的布局、包含等导致问题的情况。而且当您考虑它时,将其放在那里并没有任何意义。

    如果你想为不同的布局设置不同的值,你可以很容易地使用@dimen。对于使用你的风格的其他人来说,这也可能是相当令人惊讶的。您通常希望通过样式设置颜色、大小和行为,但大小不是其中之一。

    你永远不知道你的风格会在什么背景下被使用。这只是这些臭的东西之一,当你看到你开始觉得有些事情本可以做得更好。

    【讨论】:

    • 请考虑添加一些垂直间距(段落)。这种没有任何视觉指导的大量文本转储非常难以阅读。
    【解决方案3】:

    Build &gt; Clean Project 尝试之后,我仍然在布局编辑器中遇到同样的问题;完全重启我的 IDE 为我解决了这个问题。

    【讨论】:

      【解决方案4】:

      我试过了,效果很好。

       <EditText
              android:id="@+id/edt_mobile_no"
              style="@style/login_input_fields"
              android:hint="@string/hint_mobile"
              android:inputType="phone" />
      

      这里是 EditText 的样式

      <style name="login_input_fields">
          <item name="android:layout_width">match_parent</item>
          <item name="android:layout_height">40dp</item>
          <item name="android:layout_marginBottom">10dp</item>
          <item name="android:background">@drawable/bg_curved_white_border</item>
          <item name="android:paddingRight">10dp</item>
          <item name="android:paddingLeft">10dp</item>
          <item name="android:textColor">@android:color/white</item>
          <item name="android:textColorHint">@color/underline</item>
          <item name="android:textSize">16sp</item>
          <item name="android:typeface">monospace</item>
      </style>
      

      【讨论】:

        【解决方案5】:

        我觉得你应该把标签的父级设置为“@android:style/Widget.TextView”

        【讨论】:

        • 我们不需要为此添加标签,我们可以在样式中添加宽度和高度。
        猜你喜欢
        • 2013-06-09
        • 1970-01-01
        • 2012-10-30
        • 2017-07-14
        • 2013-10-07
        • 2015-04-07
        • 2016-05-19
        • 1970-01-01
        • 2011-06-28
        相关资源
        最近更新 更多