Question

Sorry for such a basic question but I am new to Android development, most my experience has been in c# or c++. Not sure if this is a Java thing or just Android but I have been disappointed to see that it is all held together with strings IE:

android:layout_height="wrap_content"

I would have expected to be more like:

android:layout_height=layout.wrap_content

which would make sure only valid options are used.

This seems to be the case for most things and it seems like bad practice. It also makes intellisense type stuff difficult also.

Can anyone explain why that approach has been taken? Is it a Java thing? or does it have its advantages for another reason?

Était-ce utile?

La solution 2

Well, layouts in Android are defined via XML, so it is neither Java nor Android. Since XML - is just a document markup language (just a set of rules for writing a document), it doesn't have anything like objects, methods, etc. It is not a programming language, so no enums here :)

That's essentially the whole point of XML (and any other markup language, like HTML f.i.) - it can (and should) be parsed no matter what system you use, what hardware and what OS - it is just a text written according to certain rules.

Internally Android parses this XML layout and transforms to Java objects (enums, ints, etc.)

Modern IDEs (Eclipse, Android Studio) help you a lot when you write Android XML layout - you don't need to remember all these XML values - intellisense suggests available text values for certain tags (like you have with enums) and when you mistyped some attribute - it starts complaining about invalid value. But this is not more than just an "on-the-fly" XML parser which just validates your input.

Autres conseils

It is an enum, see the attached code below. This isn't a Java thing, you posted is XML. If you want decent "intellisense", try Android Studio.

<!-- This is the basic set of layout attributes that are common to all
     layout managers.  These attributes are specified with the rest of
     a view's normal attributes (such as {@link android.R.attr#background},
     but will be parsed by the view's parent and ignored by the child.
    <p>The values defined here correspond to the base layout attribute
    class {@link android.view.ViewGroup.LayoutParams}. -->
<declare-styleable name="ViewGroup_Layout">
    <!-- Specifies the basic width of the view.  This is a required attribute
         for any view inside of a containing layout manager.  Its value may
         be a dimension (such as "12dip") for a constant width or one of
         the special constants. -->
    <attr name="layout_width" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>

    <!-- Specifies the basic height of the view.  This is a required attribute
         for any view inside of a containing layout manager.  Its value may
         be a dimension (such as "12dip") for a constant height or one of
         the special constants. -->
    <attr name="layout_height" format="dimension">
        <!-- The view should be as big as its parent (minus padding).
             This constant is deprecated starting from API Level 8 and
             is replaced by {@code match_parent}. -->
        <enum name="fill_parent" value="-1" />
        <!-- The view should be as big as its parent (minus padding).
             Introduced in API Level 8. -->
        <enum name="match_parent" value="-1" />
        <!-- The view should be only big enough to enclose its content (plus padding). -->
        <enum name="wrap_content" value="-2" />
    </attr>
</declare-styleable>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top