У меня проблема со странным поведением макетов и их идентификаторов.
Допустим, у меня есть основной макет, в котором я использую 3 раза другой макет, включив его в макет xml.
КогдаЯ получаю id
кнопки, которая находится внутри включенного макета, она одинакова для всех включенных макетов.Это правильное поведение?Я хотел использовать его в OnClickListener
для распознавания нажатой кнопки.
layout_row.xml
<?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="wrap_content" >
<Button
android:id="@+id/btnDoSomething"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do something" />
[... some other views ...]
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include android:id="@+id/layout1"
layout="@layout/layout_row" />
<include android:id="@+id/layout2"
layout="@layout/layout_row" />
<include android:id="@+id/layout3"
layout="@layout/layout_row" />
[... some other views ...]
</LinearLayout>
MainActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.info
class MainActivity : AppCompatActivity(), AnkoLogger {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
info("${layout1.btnDoSomething.id}")
info("${layout2.btnDoSomething.id}")
info("${layout3.btnDoSomething.id}")
//it logs the same id three times!
}
}