Так что сейчас у меня есть main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.b"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background_color"
android:textColorPrimaryInverse="#E97C03"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/background_color"
android:textColorPrimaryInverse="#E97C03"
>
<ImageView
android:id="@+id/header_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
android:layout_gravity="left"
android:background="@color/seperator_bg_color"
/>
<ImageView
android:id="@+id/build_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_camera"
android:layout_gravity="left"
/>
<ImageView
android:id="@+id/saved_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_details"
/>
<ImageView
android:id="@+id/settings_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_menu_upload"
/>
</LinearLayout>
<TextView
android:id="@+id/seperator_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="1dip"
android:background="@color/seperator_bg_color"
android:textColor="@color/seperator_text_color"
android:textStyle="bold"
android:text="@string/header_build"
android:textColorPrimaryInverse="#E97C03"
/>
<!-- <TextView -->
<!-- android:id="@+id/selection" -->
<!-- android:layout_width="fill_parent" -->
<!-- android:layout_height="wrap_content" -->
<!-- />-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:textColorPrimaryInverse="#E97C03"
/>
</LinearLayout>
Теперь, конечно, все, что использует main.xml, будет иметь заголовок вверху (все, что содержится во втором горизонтальном LinearLayout.
Однако во всех этих представлениях у меня есть ссылки с header_image, save_image, build_image и т. Д., Которые приведут пользователя к определенному представлению. Проблема в том, что если я внесу изменение, мне придется перейти и отредактировать каждое действие, содержащее эти ссылки.
Есть ли способ, которым я могу создать внешний класс, который будет просто содержать такую информацию:
build =(ImageView)findViewById(R.id.build_image);
build.setOnClickListener(buildClick);
saved =(ImageView)findViewById(R.id.saved_image);
saved.setOnClickListener(savedClick);
prefs =(ImageView)findViewById(R.id.settings_image);
prefs.setOnClickListener(prefsClick);
вместо того, чтобы иметь этот код в каждом действии?
РЕДАКТИРОВАТЬ: По сути, я хочу повторно использовать мой main.xml в качестве заголовка для всех действий, однако, где я устанавливаю setOnClickListener, я не хочу устанавливать их в каждом действии, потому что если я изменяю / добавляю в заголовок, Я должен изучить все действия и изменить их.
Итак, я хочу взять приведенный выше код и поместить его в отдельный класс. Таким образом, в действии я вызываю этот класс, и он устанавливает эти значения для заголовка. Или установите настройки onClick в XML, если это возможно.