Android 为什么不应该';我不能在样式中设置布局宽度和布局高度吗?

Android 为什么不应该';我不能在样式中设置布局宽度和布局高度吗?,android,eclipse,android-layout,adt,Android,Eclipse,Android Layout,Adt,如果我有很多文本视图作为项目标签,我想我可以将它们的所有共性提取到一个样式中,并且只在布局中使用 <TextView style="@style/label" android:text="Foo"/> <TextView style="@style/label" android:text="Bar"/> 风格如下: <style name="label"> <item name="android:layout_width">wrap_

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

<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编辑器抱怨说,
“”没有设置所需的布局高度属性。

报告这一警告有什么原因吗

您是否尝试清理项目

我还在一些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>

水平中心
文本多行
填补家长的空缺
包装内容
14便士
@颜色/黑色
8dp
8dp

我想你应该把标签的父项放在“@android:style/Widget.TextView”

在一次
构建>清理项目的尝试之后,我仍然在版面编辑器中遇到同样的问题;完全重新启动IDE为我解决了问题。

我已经尝试过了,但效果很好

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

下面是编辑文本的样式

<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>

匹配父项
40dp
10dp
@可拉深/bg\u弧形\u白色\u边框
10dp
10dp
@android:彩色/白色
@颜色/下划线
16便士
单空间
旧主题,但仍然:)

我认为为样式设置宽度和高度不是一个好主意。基本上,它是编译的,并且通常可以工作,但是我遇到过更复杂的布局,包括等等的情况,它会导致问题。当你想起来的时候,把它放在那里是没有意义的

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


你永远不知道你的风格会在什么环境下使用。这只是其中一个很难闻的东西,当你看到它时,你开始觉得有些事情本可以做得更好。

它工作正常,但当我切换到图形布局时,它会在Eclipse中触发一个警告对不起,我误解了。我检查了图形布局,但没有看到任何关于此的警告。但在另一方面,我也发现了关于主题的警告(
无法找到当前主题的主题资源类别
),这些主题实际上在应用程序上正常工作。可能是与Eclipse相关的问题……您解决过这个问题吗?我也看到了这一点,但当我运行应用程序时,它崩溃了,而以前它没有。似乎Eclipse把资源弄得一团糟,无法为我链接样式,但我无法修复它。清理、重新启动eclipse和其他一些事情并没有解决我的问题。有人找到了解决方案吗。最近,我们将项目从Ant build转移到Gradle,应用程序崩溃了。我们已经在样式中定义了宽度和高度参数,并将它们移回布局xml解析。但是我们现在有大量的XML,这是一个乏味的过程job@MRX我也在使用Gradle,在一些样式中我仍然有高度和宽度参数。我不知道Gradle和它有什么关系,我不认为它与你的问题有关。我们不需要添加标签,我们可以在样式中增加宽度和高度。请考虑增加一些垂直间隔(段落)。这种没有任何视觉指导的大量文本转储非常难以阅读。