0

这是我测试自定义布局(显示图像和标签)的来源。

XML 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/screen_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#3535ff"
    android:orientation="vertical"
    android:padding="1dp" >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:padding="20dp" >
        <ImageView
            android:id="@+id/screen_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:background="#000000"
            android:scaleType="centerInside"
            android:src="@drawable/cam_1_20130117_105601_118" />
        <TextView
            android:id="@+id/screen_label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/screen_image"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="3dp"
            android:background="#95000000"
            android:gravity="center_vertical|center_horizontal"
            android:text="사출성형기 1호기"
            android:textColor="#ffffff" />
    </RelativeLayout>
</LinearLayout>

JAVA代码

package com.example.testlayout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.testscreen.R;

public class CustomScreen extends LinearLayout {
    LinearLayout mLayout = null;
    TextView mLabel = null;
    ImageView mImage = null;

    public CustomScreen(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();     
    }

    public CustomScreen(Context context, AttributeSet attrs) {
        super(context, attrs);  
        init();     
    }

    public CustomScreen(Context context) {
        super(context);
        init();     
    }   

    void init() {
        LayoutInflater inflater = (LayoutInflater)getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.screen, this, true);
    //  mLabel = (TextView)findViewById(R.id.screen_label);
    //  mImage = (ImageView)findViewById(R.id.screen_image);

        if (isInEditMode()) {
            return;
        }

    }
}

使用此代码,我检查了 Nexus One。它显示得很好。问题在于编辑器模式。正是在带有预览的 xml 编辑器上。当我添加此视图时,出现错误消息。

无法实例化以下类: - com.example.testlayout.CustomScreen(打开类,显示错误日志)有关详细信息,请参阅错误日志(窗口 > 显示视图)。提示:当在 Eclipse 中显示时,在自定义视图中使用 View.isInEditMode() 以跳过代码

我想在编辑模式下检查它。我怎样才能像其他视图一样检查好?

4

1 回答 1

3

您必须在每个构造函数中添加 (!isInEditMode())。它告诉它是否不在编辑模式然后初始化这些东西

 if(!isInEditMode())
      init(context);

并为初始化东西遵循这个

看看这个

于 2015-06-01T11:34:29.997 回答