нормально
то, что я пытаюсь сделать, - это вставить пользовательский вид в макет по умолчанию main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.lam.customview.CustomDisplayView
android:id="@+id/custom_display_view1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/prev"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="50"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/prev" />
</LinearLayout>
</LinearLayout>
как вы можете видеть, класс называется com.lam.customview.CustomDisplayView с идентификатором custom_display_view1.
теперь в классе com.lam.customview.CustomDisplayView, я хочу использовать другой макет под названием custom_display_view.xml, потому что я не хочу программно создавать элементы управления/виджеты.
custom_display_view.xml - это просто кнопка и изображение, содержание которого я хочу изменить на основе определенных условий:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/display_text_view1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/display_image_view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
</LinearLayout>
Я попытался сделать:
1)
public CustomDisplayView(Context context, AttributeSet attrs) {
super(context, attrs);
try
{
// register our interest in hearing about changes to our surface
SurfaceHolder holder = getHolder();
holder.addCallback(this);
View.inflate(context, R.layout.custom_display_view, null);
...
но получил эту ошибку ", 03-08 20: 33: 15.711: ERROR/onCreate (10879): двоичная строка XML файла # 8: ошибка раздувания класса java.lang.reflect.Constructor".
2)
public CustomDisplayView(Context context, AttributeSet attrs) {
super(context, attrs);
try
{
// register our interest in hearing about changes to our surface
SurfaceHolder holder = getHolder();
holder.addCallback(this);
View.inflate(context, R.id.custom_display_view1, null);
...
но получил эту ошибку ", 03-08 20: 28: 47.401: ERROR/CustomDisplayView (10806): Идентификатор ресурса # 0x7f050002 type # 0x12 недействителен"
также, если я делаю это так, как предложил кто-то, мне не ясно, как custom_display_view.xml связан с классом пользовательского вида.
спасибо.