2017-01-14 9 views
3

내가이 클래스 MyView을 말해봐 인레이가 setContentView 인 경우 initMyView으로 호출되기 전에 mRenderer이 아직 초기화되지 않았습니다.뷰의 속성에 따라 물건을 초기화하는 방법

속성이 설정된 후에 발생하는보기의 초기화 없이는보기의 속성을 설정할 수 없으므로 catch 22처럼 보입니다. 당신이 그것을하고있는 같다

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/my_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@drawable/background" > 

    <com.developer.app.MyView 
     android:id="@+id/my_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/background" 
     /> 

    <TextView 
     android:id="@+id/left_arrow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/left_arrow" 
     /> 

    <TextView 
     android:id="@+id/right_arrow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/right_arrow" 
     /> 

    <TextView 
     android:id="@+id/home_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/home" 
     /> 
</RelativeLayout> 
+0

실제로이 코드를 실행 했습니까? 'MyView' 뷰가 레이아웃 XML'R.layout.my_layout'의 일부라고 가정합니다. 그렇다면'setContentView'는'MyView'의 생성자를 호출하게 만들 것입니다. 어쨌든 모든 장점으로'setBackground'는 인스턴스 메소드이며 렌더러가 초기화되기 전에 코드 흐름에서 호출 할 수있는 방법이 없습니다 (생성자에서 렌더러를 초기화하는 중입니다) – Dibzmania

+0

@Dibzmania이 코드는 주석을 달고 있습니다. 'MyView'에서'setBackground'를 꺼내거나 수퍼 클래스의'setBackground' 메소드를 호출하는 것입니다. 이전의보기는 검정색 배경이됩니다. 이후에는 렌더링 클래스의 onDrawFrame 메소드에 모든 draw 호출이있을 때에도 배경이 수퍼 클래스에 의해 렌더링되지만 내 지오메트리는 렌더링되지 않습니다. – rraallvv

+0

생성자 또는 init 메소드에서'setZOrderOnTop (true)'를 호출 해보십시오. 그리고 나서'setBackground' 메쏘드의 주석 처리를 제거하고 작동하는지 확인하십시오. – Dibzmania

답변

1

당신은 맞다. setBackground()android:background 속성이 구문 분석 된 경우 View의 생성자에서 호출됩니다. 귀하의 경우는 여기라고 : 당신이 mRenderer에 배경을 설정하려면

super(ctx, attrs); 

, 당신이 init에서 작업을 수행 할 수 있습니다

private void init(Context ctx) { 
    mRenderer = new MyRenderer(ctx); 
    setRenderer(mRenderer); 
    final Drawable background = getBackground(); 
    if (background != null) { 
     mRenderer.setBackground(background); 
    } 
} 

setBackground에 널 (null) 검사를 추가하기 때문에이 방법은 수 어떤 값을 설정하기 전에 수퍼 클래스의 생성자에서 호출해야합니다.

@Override 
public void setBackground(Drawable background) { 
    if (mRenderer != null) { 
     mRenderer.setBackground(background); 
    } 
} 
0

당신은 예와 XML과 자바에 대한 세 가지 기능을 초기화가 :

레이아웃입니다.

public class MyEditText extends EditText { 
    public MyEditText(Context context) { 
     super(context); 
     this.setTextColor(Color.parseColor("#4789da")); 
     this.setBackgroundResource(R.drawable.edittext_back); 
     this.setTextSize(18); 
     this.setPadding(5,5,5,5); 
    } 

    public MyEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.setTextColor(Color.parseColor("#4789da")); 
     this.setBackgroundResource(R.drawable.edittext_back); 
     this.setTextSize(18); 
     this.setPadding(5,5,5,5); 
    } 

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     this.setTextColor(Color.parseColor("#4789da")); 
     this.setBackgroundResource(R.drawable.edittext_back); 
     this.setTextSize(18); 
     this.setPadding(5,5,5,5); 
    } 
} 
1

귀하의 질문에서 이해 한 사항. oncreate()에서 뷰의 속성이 필요합니다. 그러나 그렇게 할 수 없다. 그 이유는 UI가 아직 초기화되지 않았기 때문입니다. 주위에 해결 방법이 있습니다. 이 링크가 도움이 될 수 있습니다.

getWidth() and getHeight() of View returns 0