2012-01-20 4 views
8

このCommonsWareの例によると、私のRelativeLayoutサブクラスを、マージルートを持つxmlレイアウトで記述された私のレイアウトとマージすることができました。私の唯一の関心事は、XMLでRelativeLayoutパラメータを記述できないことです。MergeレイアウトからRelativeLayoutへのinflateによるXML属性

私の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"/> 

私はこの問題は、マージがマージタグではなく、マージ自身の子供たちに起こっていることだと思います。どのように私のRelativeLayoutに影響を与えるには、マージの私のパラメータを得ることができますか?

パッケージ宣言や輸入なし

マイRelativeLayoutサブクラス、:

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); 
    } 
} 

私はLayoutParamsにすべてを追加し、そのLayoutParamsと私のMyRelativeLayoutSubclassを追加することができます知っているが、私はそれはたくさんだ、脱出をしたいと思います不要なコードの

答えて

17

私は、マージはマージタグ自体のものではなく、マージタグの子に起こると思います。

AFAIK、あなたは正しいです。 <merge>はプレースホルダで、ViewGroupではありません。

私はLayoutParamsにすべてを追加し、そのLayoutParamsと私のMyRelativeLayoutSubclassを追加することができます知っているが、私はそれが不要なコードがたくさんだ、脱出をしたいと思います。

MyRelativeLayoutSubclass要素を含むXMLレイアウトファイルを作成し、そこに属性を配置します。その後、そのレイアウトを膨張させます。

+0

これは非常に明白な解決策であり、なぜ私はそれを逃したのかわかりません。 – gmate

+0

しかし、あなたは簡単にデフォルトの属性を持つことはできません。カスタムコンポーネントを使用するたびに再入力する必要があります。 – Timmmm

+0

@Timmmm: 'MyRelativeLayoutSubclass'は属性のデフォルト値を持つことができます。属性をXMLから読み込む際に使用されます。 – CommonsWare

1

すべての属性をスタイルに抽出することは私の解決策でした。ハードコードされた属性やJavaコードとは異なり、のスクリーンサイズに依存するです。たぶんあなたはさらにテーマの属性に入れることができます。

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

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

アトリビュートとプログラム上のLayoutParamsは、画面サイズに依存します。 – Xiao

関連する問題