2013-01-17 113 views
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()在您的自定義視圖跳躍代碼。 如何查看其他視圖?

+0

這就是下面的副本。檢查 http://stackoverflow.com/a/12841681/1460545 –

+0

哦..我再次檢查並解決。 ** isInEditMode()** - > ** this.isInEditMode()**謝謝 – Temp

回答

3

您必須在每個構造函數中添加(!isInEditMode())。它告訴,如果它不是在編輯模式,然後初始化的東西

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

和初始化的東西follow this

也有look at this