Атрибут настройки дочернего элемента включенного макета
У меня есть файл main.xml, описывающий макет моей основной деятельности:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
<include layout="@layout/mylayout" />
</LinearLayout>
и его включенный макет xml файла (mylayout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello world" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Я просто хочу включить "mylayout" в свой основной макет 5 раз, но вместо того, чтобы видеть "привет мир" 5 раз, я хочу, чтобы TextView содержал пользовательский текст.
Есть ли способ сделать это, установив некоторый атрибут элемента include, чтобы переопределить текст Text Text для детей? Каким будет наилучший подход для достижения этого?
Ответы
Ответ 1
Нет, нет способа передать параметры включенному макету, кроме параметров макета, с помощью директивы <include>
.
Вы можете раздуть макет программно и добавить их в свой вид. Добавьте id в контейнер в вашем основном макете:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" />
Затем в вашей деятельности:
ViewGroup container = (ViewGroup)findViewById(R.id.container);
for (int i = 0; i < 6; i++) {
View myLayout = getLayoutInflater.inflate(R.layout.mylayout, null);
TextView tv = myLayout.findViewById(R.id.textView);
tv.setText("my layout " + i);
container.addView(myLayout); // you can pass extra layout params here too
}
Ответ 2
https://developer.android.com/training/improving-layouts/reusing-layouts.html
Вы можете установить свойство android: id каждого из них. Это дает идентификатор корневому элементу планируемого макета.
Получите это представление по id, затем найдите подпункт, в который вы хотите изменить текст.
Ответ 3
Возможно, если вы активируете привязку данных
reuse_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="text" type="String"/>
</data>
<LinearLayout
android:id="@+id/mylayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{text}" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
когда вы вызываете reuse_layout.xml, просто добавьте к нему атрибут "app: text"
<include layout="@layout/reuse_layout" app:text="some tex" />