34

If I have a lot of TextViews serving as item labels, I thought I could extract everything that's common about them into a style, and in the layout use only

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

with style like:

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

But when I do this, it works fine on emulator and device, but the Android XML editor complains that "<TextView>" does not set the required layout_height attribute. Is there some reason on why it is reporting this warning?

Axarydax
  • 16,353
  • 21
  • 92
  • 151

5 Answers5

10

Have you tried to clean the project?

I am using also the same thing on some XML files for the width and the length. Here is an example working on my app, you can have a look:

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

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

Yoann Hercouet
  • 17,894
  • 5
  • 58
  • 85
  • it is working fine, but it triggers a warning in Eclipse when I switch into Graphical Layout – Axarydax May 11 '13 at 16:11
  • Sorry I misunderstood. I checked in the graphical layout but did not see any warning about this. But on the other side I also found warnings about themes (`Couldn't find theme resource categories for the current theme`) that are actually working properly on the app. Maybe a problem related to Eclipse... – Yoann Hercouet May 11 '13 at 16:30
  • Did you ever resolve this issue? I too am seeing this but it crashes when I run the app when previously it did not. Seems like Eclipse has jumbled the resources and failed to link styles for me but I can't fix it. Cleaning, restarting eclipse and a few other things have not resolved my issue. – Cookster Apr 08 '14 at 22:45
  • Did any1 found a solution for this. Recently we moved our project from Ant build to Gradle based and the app crashes. We have defined the width and height parameters in style and moving them back to layout xml resolves. But we have tons of XML now and its a tedious job – MRX May 20 '16 at 09:10
  • @MRX I am also using Gradle and I still have height and width parameters in some styles. I don't see what Gradle has to do with it, I don't think it is related to your problems. – Yoann Hercouet May 20 '16 at 09:13
  • @ Yoann Hercouet Ok may be you are correct, even we doubt that it has nothing to do with Gradle. Any idea how to resolve this, we hardly use any native views and replacing the parameter from style to base layout will take a lot of time. I have been reading about it but I guess I am in the wrong direction – MRX May 20 '16 at 09:16
  • @MRX You should maybe just create a question and explain your problem with code samples – Yoann Hercouet May 20 '16 at 09:17
3

After a Build > Clean Project attempt, I was still experiencing this same issue in the layout editor; a full restart of my IDE solved the problem for me.

jterry
  • 6,209
  • 2
  • 30
  • 36
3

Old topic but still :)

I don't think setting a width and height to style is a good idea. Basically it compiles and usually works but I experienced situations with more complex layouts, includes etc. where it was causing problems. And when you think about it, it doesn't really make sense to put it there.

You can easily use @dimen if you want different values for different layouts. And it can also be pretty surprising for somebody else using your style. You usually want to set color, size, and behavior with style but size is not one of the things.

You never know in what context your style gonna be used in. It's just one of these smelly things which when you see you start to have a feeling that something could have been done better.

bio007
  • 893
  • 11
  • 20
  • 1
    Please consider adding some vertical spacing (paragraphs). Such massive text dumps without any visual guidance are very hard to read. – GhostCat Aug 20 '18 at 12:00
0

I have tried this, its just working fine..

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

Here below the style for the 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>
Raja Jawahar
  • 6,742
  • 9
  • 45
  • 56
-3

I thing you should put the parent of label as "@android:style/Widget.TextView"

Foad Saeidi Nik
  • 260
  • 1
  • 5
  • 20