【发布时间】:2018-04-22 11:01:58
【问题描述】:
我很困惑为什么我们需要 + 在一些同级引用中而不是其他引用中以使视图在 android studio 预览窗格中正确呈现。 (在正在运行的应用中查看时的行为不同,因此不在范围内)
来自 ID 部分下的the android guide docs:The plus-symbol (+) means that this is a new resource name that must be created and added to our resources (in the R.java file).
来自the Relative Layout Params docsandroid:layout_toLeftOf
Positions the right edge of this view to the left of the given anchor view ID. May be a reference to another resource, in the form "@[+][package:]type/name"
In your XML layout, dependencies against other views in the layout can be declared in any order. For example, you can declare that "view1" be positioned below "view2" even if "view2" is the last view declared in the hierarchy. The example below demonstrates such a scenario.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name" <- no + as 'name' already declared?
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" /> <- + present as times not declared yet?
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done" />
</RelativeLayout>
所有这些信息让我相信 + 符号仅在引用兄弟姐妹时使用,如果兄弟姐妹在引用之后声明?
但在我的项目中似乎并非如此:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_grey_light"
android:paddingBottom="10dp">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="220dp"
android:adjustViewBounds="true" />
<TextView
android:id="@+id/some_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img" <- This needs a + to render correctly..
android:text="@{some.name}"/>
</RelativeLayout>
那么+ 符号的规则是什么,有没有我遗漏的可靠文档?
【问题讨论】:
标签: android android-relativelayout android-identifiers