1

편의상 분리 된 다른 사용자 지정 UI 구성 요소가있는 사용자 지정 UI 구성 요소가 있습니다.사용자 지정 UI 구성 요소의 XML 사용자 지정 특성을 사용자 지정 UI 하위 구성 요소로 전달

아버지 구성 요소에 내 자신의 특성을 전달하고 하위 구성 요소 내에서 읽을 수 있기를 원합니다. 그런 식으로 개발자가 알아야 할 유일한 것은 내부에 다른 구성 요소가 있다는 것을 알 필요가없는 아버지 구성 요소입니다.

예를 들어

,이 응용 프로그램의 주 레이아웃입니다 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:udinic="http://schemas.android.com/apk/res/com.udinic" 
android:id="@+id/main" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 

    <com.udinic.FatherComponent android:id="@+id/fatherComp" 
    android:layout_width="fill_parent" 
    udinic:itemNum="9" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

와 아버지 구성 요소의 XML은 다음과 같습니다 :이이 을 사용하여 할 수있는 방법을 찾을 수 없습니다

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:udinic="http://schemas.android.com/apk/res/com.udinic" 
android:id="@+id/fatherLayout" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 

    <com.udinic.SubComponent android:id="@+id/subComp" 
    android:layout_width="fill_parent" 
    udinic:itemNum=<<Get the itemNum passed to me>> 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

XML은입니다. 이 문제를 해결할 수있는 방법을 아는 사람이 있습니까?

감사

답변

0

당신은 하위 클래스로 값을 전달 아버지 클래스의 생성자를 사용해야합니다.

public FatherClass(Context context, AttributeSet attrs) 
{ 
    super(context, attrs, LAYOUT); 
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomLayout); 
    //for string attribute 
    textView.setText(getStringAttribute(typedArray, R.styleable.CustomLayout_labelText)); 
    //for resources 
    int textAppearance = typedArray.getResourceId(R.styleable.CustomLayout_textAppearance, -1); 
    textView.setTextAppearance(context, textAppearance); 
    //for integers 
    int inputType = typedArray.getInt(R.styleable.CustomLayout_inputType, -1); 
    editText.setInputType(inputType); 
}