2012-01-20 3 views
8

입니다.이 CommonsWare 예제에 따르면 나는 RelgeLayout 하위 클래스를 병합 루트가있는 xml 레이아웃에 설명 된 내 레이아웃과 병합 할 수있었습니다. 내 유일한 관심사는 XML에서 RelativeLayout 매개 변수를 설명 할 수 없다는 것입니다.병합 레이아웃에서 XML을 통한 RelativeLayout에 대한 속성은

내 XML 레이아웃 :

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:my="http://schemas.android.com/apk/res/hu.someproject" 
    android:layout_width="36dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="3dp" 
    android:layout_marginRight="3dp" 
    android:width="36dp" > 

<RelativeLayout 
    android:id="@+id/upper_container" 
    style="?pretty_style" 
    android:layout_alignLeft="@+id/image_title" 
    android:layout_alignParentTop="true" 
    android:layout_alignRight="@+id/image_title" 
    android:layout_centerHorizontal="true" > 

    <View 
     android:id="@+id/upper_indicator" 
     android:layout_width="fill_parent" 
     android:layout_height="5dp" 
     android:layout_alignParentTop="true" 
     android:background="@color/mycolor" /> 

    <TextView 
     android:id="@+id/text_degree" 
     style="?pretty_style" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="23°" /> 
</RelativeLayout> 

<hu.MyView 
    android:id="@+id/image_title" 
    style="?my_image_title" 
    android:layout_below="@+id/upper_container" 
    android:layout_centerHorizontal="true" 
    my:myColor="#2f8741"/> 

나는 병합 자체가 문제가 병합 병합 태그의 아이들은 어떻게 생각하고 있지. 어떻게하면 RelgeLayout에 영향을 미치기 위해 병합에서 매개 변수를 얻을 수 있습니까? 패키지 선언과 수입이없는

내 RelativeLayout의 서브 클래스 : 나는이의 LayoutParams에 모든 것을 추가 한 다음 그의 LayoutParams 내 MyRelativeLayoutSubclass을 추가 할 수 있지만 나는 그것을 탈출하려는 수있어

public class MyRelativeLayoutSubclass extends RelativeLayout { 

    public MyRelativeLayoutSubclass(Context context) { 
     super(context); 

     initTile(null); 
    } 

    public MyRelativeLayoutSubclass(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     initTile(attrs); 
    } 

    public MyRelativeLayoutSubclass(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     initTile(attrs); 
    } 

    private void initTile(Object object) { 
     final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.my_great_little_layout, this, true); 
    } 
} 

, 즉 많은입니다 불필요한 코드

답변

17

저는 병합이 병합 자체가 아니라 병합 태그의 자식에게 발생한다고 생각합니다.

AFAIK, 정확합니다. <merge>은 자리 표시 자이며 ViewGroup이 아닙니다.

나는 LayoutParams에 모든 것을 추가 할 수 있고, 그 다음 LayoutParams로 MyRelativeLayoutSubclass를 추가 할 수 있지만, 불필요한 코드가 많은 것을 피하고 싶다.

MyRelativeLayoutSubclass 요소가 포함 된 XML 레이아웃 파일을 만들고 여기에 속성을 넣으십시오. 그런 다음, 그 레이아웃을 팽창시킵니다.

+0

이것은 매우 분명한 해결책이며, 왜 내가 그것을 놓쳤는 지 잘 모릅니다. – gmate

+0

기본 속성을 쉽게 가질 수는 없습니다. 내 사용자 지정 구성 요소를 사용할 때마다 다시 입력해야합니다. – Timmmm

+0

@Timmmm : MyRelativeLayoutSubclass는 XML의 속성을 읽으려고 시도 할 때 (속성과 만나는 데 실패 할 때) 사용되는 속성의 기본값을 가질 수 있습니다. – CommonsWare

1

모든 속성을 스타일로 추출하면 나에게 해결책이되었습니다. 보너스로 화면 크기가 이고 하드 코드 된 속성 또는 Java 코드와 달리입니다. 어쩌면 더 멀리 가서 Theme 속성에 넣을 수 있습니다.

<com.example.widget.MyCustomView 
    android:id="@+id/my_custom_view" 

    style="@style/MyCustomView.Default"/> 
+0

속성 및 프로그래밍 방식 LayoutParams는 화면 크기에 따라 다를 수 있습니다. – Xiao

관련 문제