Java Android为什么所有的布局都是用字符串而不是枚举完成的

Java Android为什么所有的布局都是用字符串而不是枚举完成的,java,android,enums,Java,Android,Enums,很抱歉问了这么一个基本的问题,但我对Android开发还不熟悉,我的大部分经验都是在c或c++方面。不确定这是Java的还是Android的,但我很失望地看到,这一切都与字符串(即: android:layout\u height=“wrap\u content” 我本以为会更像: android:layout\u height=layout.wrap\u content 这将确保只使用有效的选项 这似乎是大多数事情的情况,这似乎是一个坏习惯。这也使得智能感知类型的东西变得困难 有人能解释为什么

很抱歉问了这么一个基本的问题,但我对Android开发还不熟悉,我的大部分经验都是在
c
c++
方面。不确定这是Java的还是Android的,但我很失望地看到,这一切都与字符串(即:

android:layout\u height=“wrap\u content”

我本以为会更像:

android:layout\u height=layout.wrap\u content

这将确保只使用有效的选项

这似乎是大多数事情的情况,这似乎是一个坏习惯。这也使得智能感知类型的东西变得困难


有人能解释为什么采取这种做法吗?它是Java的东西吗?或者它有其他优点吗?

它是一个枚举,请参阅下面的附加代码。这不是Java的东西,你发布的是。如果你想要一个像样的“智能感知”,试试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>

它是一个枚举,请参见下面的附加代码。这不是Java的东西,你发布的是。如果你想要一个像样的“智能感知”,试试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>

好吧,Android中的布局是通过定义的,所以它既不是Java也不是Android。由于XML-只是一种文档标记语言(只是一组用于编写文档的规则),因此它没有任何对象、方法等。它不是一种编程语言,因此这里没有枚举:)

这就是XML(以及任何其他标记语言,如HTML f.i.)的基本要点——它可以(也应该)被解析,无论您使用什么系统、什么硬件和什么操作系统——它只是根据特定规则编写的文本

Android在内部解析这个XML布局,并将其转换为Java对象(枚举、整数等)


当您编写Android XML布局时,现代IDE(,)对您有很大帮助—您不需要记住所有这些XML值—intellisense建议某些标记的可用文本值(就像您在枚举中所做的那样),当您键入某些属性时—它开始抱怨无效值。但这不仅仅是一个“动态”XML解析器,它只是验证您的输入。

好吧,Android中的布局是通过定义的,因此它既不是Java也不是Android。由于XML-只是一种文档标记语言(只是一组用于编写文档的规则),因此它没有任何对象、方法等。它不是一种编程语言,因此这里没有枚举:)

这就是XML(以及任何其他标记语言,如HTML f.i.)的基本要点——它可以(也应该)被解析,无论您使用什么系统、什么硬件和什么操作系统——它只是根据特定规则编写的文本

Android在内部解析这个XML布局,并将其转换为Java对象(枚举、整数等)


当您编写Android XML布局时,现代IDE(,)对您有很大帮助—您不需要记住所有这些XML值—intellisense建议某些标记的可用文本值(就像您在枚举中所做的那样),当您键入某些属性时—它开始抱怨无效值。但这不仅仅是一个“动态”的XML解析器,它只是验证您的输入。

版面不仅仅是XML文件吗?版面不仅仅是XML文件吗?谢谢,这很有意义:)谢谢,这很有意义:)