很抱歉这个基本问题,但我是Android开发新手,我的经验大部分都在c#
或c++
。不确定这是Java还是Android,但我很失望地看到它全部用字符串IE连接:
android:layout_height="wrap_content"
我希望更像是:
android:layout_height=layout.wrap_content
这将确保仅使用有效选项。
对于大多数事情来说似乎都是如此,这似乎是不好的做法。它也使智能感知类型的东西变得困难。
任何人都可以解释为什么采取这种方法?它是Java的东西吗?或者它有其他原因的优势吗?
答案 0 :(得分:4)
这是一个枚举,请参阅下面的附加代码。这不是Java的东西,你发布的是XML。如果你想要体面的“intellisense”,试试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>
答案 1 :(得分:2)
嗯,Android中的布局是通过XML
定义的,所以它既不是Java也不是Android。由于XML - 只是一种文档标记语言(只是一组编写文档的规则),它没有像对象,方法等那样的东西。它不是一种编程语言,所以这里没有枚举:)< / p>
这基本上是XML(以及任何其他标记语言,如HTML fi)的全部要点 - 无论您使用什么系统,什么硬件和什么操作系统,它都可以(并且应该)进行解析 - 它只是根据某些规则撰写的文本。
内部Android解析此XML布局并转换为Java对象(枚举,整数等)
现代IDE(Eclipse,Android Studio)在您编写Android XML布局时对您有很大的帮助 - 您不需要记住所有这些XML值 - intellisense建议可用的文本值标签(就像你有枚举一样),当你错误输入一些属性时 - 它开始抱怨无效的价值。但这不仅仅是一个&#34;在飞行中&#34; XML解析器,只验证您的输入。