пользовательское представление с расположением

хорошо,

то, что я пытаюсь сделать, должно встроить пользовательское представление в расположение по умолчанию 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. Конструктор".

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 тип #0x12 Ресурса не допустим"

также, если я делаю это этот путь, как кто-то предположил, мне не ясно, как custom_display_view.xml связан с пользовательским классом представления.

спасибо.

34
задан Quintin Robinson 9 March 2010 в 07:29
поделиться

2 ответа

Попробуйте использовать

context.getLayoutInflater().inflate( R.id.custom_display_view1, null );
0
ответ дан 27 November 2019 в 16:15
поделиться

Вы можете добавить cutom_display_view в свой собственный класс следующим образом:

public CustomDisplayView(Context context, AttributeSet attrs) {   
    super(context, attrs);           
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            if(inflater != null){       
                inflater.inflate(R.layout.custom_display_view, this);
            }
}
7
ответ дан 27 November 2019 в 16:15
поделиться
Другие вопросы по тегам:

Похожие вопросы: