【问题标题】:Referencing sibling layouts in Android Relative Layout (xml preview)在 Android 相对布局中引用同级布局(xml 预览)
【发布时间】:2018-04-22 11:01:58
【问题描述】:

我很困惑为什么我们需要 + 在一些同级引用中而不是其他引用中以使视图在 android studio 预览窗格中正确呈现。 (在正在运行的应用中查看时的行为不同,因此不在范围内)

来自 ID 部分下的the android guide docsThe 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"

来自Relative Layout Examples

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


    【解决方案1】:

    来自docs:

    字符串开头的 at 符号 (@) 表示 XML 解析器应该解析和扩展 ID 字符串的其余部分,并且 将其标识为 ID 资源。加号 (+) 表示这是 必须创建并添加到我们的资源中的新资源名称 (在 R.java 文件中)。还有许多其他 ID 资源 由 Android 框架提供。引用 Android 时 资源ID,不需要加号,但必须加android 包命名空间,如下所示:

    android:id="@android:id/empty"
    

    在创建视图对象时定义视图对象的 ID 很重要 相对布局。在相对布局中,兄弟视图可以定义它们的 相对于另一个兄弟视图的布局,该视图由 唯一 ID。

    ID 在整个树中不必是唯一的,但应该是 在您正在搜索的树的一部分中是唯一的(通常可能 是整棵树,所以最好是完全唯一的 可能)。

    希望对你有所帮助。

    在预览模式下,在您的情况下,它应该在没有+ 的情况下呈现。

    尝试在根布局中添加工具命名空间:

    xmlns:tools="http://schemas.android.com/tools"
    

    然后在你的TextView:

    tools:text="Some name"
    

    【讨论】:

    • 谢谢,我已经阅读了文档,并且我已经包含了工具:文本(为了简单起见,将其取出)。我认为它应该在没有 + 的情况下呈现,但它没有:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 2017-12-19
    • 2018-12-03
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 2015-01-01
    相关资源
    最近更新 更多