【问题标题】:How do I add the same id twice in my xml?如何在我的 xml 中添加两次相同的 id?
【发布时间】:2013-11-16 21:02:36
【问题描述】:

我试图在 xml 中添加相同的 id 两次(彼此相邻),但它一直向我显示错误并且无法按预期工作。(我想将这两个代码块放在每个旁边其他而不是其他,我对android很陌生,对此一无所知,有什么想法吗?

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res/com.justin.jar"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#004D79"
    >
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/news_headline_padding" >

    <com.justin.jar.utils.FontTextView
        android:id="@+id/news_headline_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="3"
        android:paddingLeft="@dimen/common_left_padding"
        android:paddingRight="5dp"
        android:textSize="16sp" 
        foo:customFont="cabin.medium.ttf"
        android:textColor="@color/search_autosuggest_header_text"
        android:layout_toRightOf="@id/news_headline_image" />

    <com.justin.jar.utils.FontTextView
        android:id="@+id/metadata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/search_autosuggest_item_subtitle"
        android:textSize="12sp" 
        android:paddingLeft="@dimen/common_left_padding"
        android:paddingRight="5dp"
        android:layout_alignLeft="@id/news_headline_text"
        android:layout_toRightOf="@id/news_headline_image"
        android:layout_below="@id/news_headline_text" 
        />


    <ImageView
        android:id="@+id/news_headline_image"
        android:layout_width="107dp"
        android:layout_height="80dp"
        android:background="@drawable/container_dropshadow"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:paddingTop="1dp"
        android:paddingBottom="1dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/common_left_padding"
        android:contentDescription="@string/headline_image" 
        />
</RelativeLayout>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/news_headline_padding" >

    <com.justin.jar.utils.FontTextView
        android:id="@+id/news_headline_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="3"
        android:paddingLeft="@dimen/common_left_padding"
        android:paddingRight="5dp"
        android:textSize="16sp" 
        foo:customFont="cabin.medium.ttf"
        android:textColor="@color/search_autosuggest_header_text"
        android:layout_toRightOf="@id/news_headline_image" />

    <com.justin.jar.utils.FontTextView
        android:id="@+id/metadata"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/search_autosuggest_item_subtitle"
        android:textSize="12sp" 
        android:paddingLeft="@dimen/common_left_padding"
        android:paddingRight="5dp"
        android:layout_alignLeft="@id/news_headline_text"
        android:layout_toRightOf="@id/news_headline_image"
        android:layout_below="@id/news_headline_text" 
        />


    <ImageView
        android:id="@+id/news_headline_image"
        android:layout_width="107dp"
        android:layout_height="80dp"
        android:background="@drawable/container_dropshadow"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:paddingTop="1dp"
        android:paddingBottom="1dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/common_left_padding"
        android:contentDescription="@string/headline_image" 
        />
</RelativeLayout>
</FrameLayout>

这是我想要的示例 http://i.stack.imgur.com/TgfZl.png

[1]:

【问题讨论】:

  • 嗯...给他们不同的ids?
  • 理解 XML 不是代码可能会有所帮助。它是数据,数据的格式是由一定的规则决定的。您不能随心所欲地重新排列“块”。
  • 这就是我需要的,我该如何布局呢? i.stack.imgur.com/TgfZl.png
  • 链接的图像似乎与这个问题无关。烂网址?

标签: android android-layout android-fragments android-linearlayout


【解决方案1】:

From the Docs

身份证

任何 View 对象都可能有一个与之关联的整数 ID,以唯一 识别树中的视图。

id改成一个。比如

<com.justin.jar.utils.FontTextView
android:id="@+id/news_headline_text_right"

我相信,您可以在不同的resource 文件中拥有具有相同idViews,尽管我认为这不是一个好主意。但是在树中它们必须是唯一的,否则当您尝试使用 findViewById() 访问它时,Android 将不知道该使用哪一个。

【讨论】:

  • 这不是真的:即使在一个布局文件中也可以有相同的 id,为什么不呢?
  • @pskink 错误。尝试在一个布局文件中将任何侦听器附加到具有多个参考点的 ID,您的应用将崩溃。
  • 我只是想将 2 个 relativelayout 块彼此相邻放置,但它们使用的 java 代码中的 id 与 news_headlines 中的 id 相同,我无法在 java 代码中更改它,因为我正在修改只是对于表格,保持电话代码相同,因此布局中的这些更改位于 large 和 xlarge 文件夹下,知道如何解决这个问题吗?
  • 你的意思是 for.ex。点击监听?如果是这样,您将其添加为视图而不是 ID
  • @JusticeBauer 恐怕我不明白你在说什么......你能更好地解释你想要改变什么以及为什么你希望ids 相同吗?
【解决方案2】:

ID 属性必须是项目中每个对象的唯一属性。所以如果你用相同的ID创建两个或多个对象,这不会产生任何错误,但编译器会使用他遇到的第一个。你为什么还需要这个??

【讨论】:

    【解决方案3】:

    ID 的不能在一个 XML 文档中多次使用。它们必须是独一无二的。

    【讨论】:

    • @pskink 错误。尝试在一个布局文件中将任何侦听器附加到具有多个参考点的 ID,您的应用将崩溃。
    猜你喜欢
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    相关资源
    最近更新 更多