0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

layoutの@idに+をつける理由

Posted at

AndroidのLayoutにID名を指定するが、その時に+をつける理由をメモします。

            <!-- ↓この'+'部分 -->
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="20dp"
    android:layout_gravity="center"
    android:textSize="16sp"
    android:text="表示テスト"/>

公式ドキュメントでは

文字列の先頭にあるアット記号(@)は、 XML パーサーは ID 文字列の残りの部分を解析して展開し、 定義できます。プラス記号(+)は、新しいリソース名であることを示します。 これを作成して R.java 内のリソースに追加する必要がある 表示されます。

と書かれています。Javaに新しいリソースを生成してるみたい。
私の環境では+は使用しなくてもビルドは通り正常に動作します。IDEの方でうまく補完しているかもしれません。
ConstraintLayoutなどでIDの指定をする場合、+は書かずに指定します。ですがこちらも同じく+を書いたとしてもエラー無く動作しますので、絶対に書いてはいけないこともなさそう。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?