2012-01-19 8 views
0

私はすべてのコンストラクタでRelativeLayoutを拡張しました。そして、私は自分のクラスのコンストラクタの1つで膨張しているxmlのレイアウトを作成しました。私は拡張ビューが正しく動作しない

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <EditText 
     android:id="@+id/clearable_edit" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     /> 
    <Button 
     android:id="@+id/clearable_button_clear" 
     android:layout_width="30dip" 
     android:layout_height="30dip" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/image_clear" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="5dip"/> 
    </RelativeLayout> 

を膨らませると、私はこれが正常に動作します。この

<com.and.MyClass 
    android:id="@+id/my_class" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

ように私の活動のレイアウトでのMyClassを使用MyClassの

public class MyClass extends RelativeLayout 
{ 
     LayoutInflater inflater = null; 
     EditText edit_text; 
     Button btn_clear; 

    public MyClass(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 
     public MyClass(Context context, AttributeSet attrs) 
     { 
      super(context, attrs); 
      // TODO Auto-generated constructor stub 

      inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inflater.inflate(R.layout.my_class_layout, this, true); 
      edit_text = (EditText) findViewById(R.id.clearable_edit); 
      btn_clear = (Button) findViewById(R.id.clearable_button_clear); 
      btn_clear.setVisibility(RelativeLayout.INVISIBLE); 
     } 

     public MyClass(Context context) 
     { 
      super(context); 
      // TODO Auto-generated constructor stub 
     } 
    } 

私のクラスのレイアウトのための

マイコード。 しかし、コンストラクタMyClass(コンテキストコンテキスト)でクラスレイアウトを膨張させると、動作しません。

問題が発生していません。私はあなたに私の質問があることを願っています

答えて

2

これはOKです。 我々はXMLで

public MyClass(Context context, AttributeSet attrs) 

Viewを使用する場合、このコンストラクタが呼び出されます。

AttributeSetを使用してXMLのすべての属性がこのコンストラクタに渡されるため、これらの値はViewのレイアウトやその他のフィールドに影響します。あなたはまた、JavaであなたのViewを使用したい場合は

しかし、あなたはまた、すべてのViewのコンストラクタ(アプローチ推奨され)で膨らませる必要があります。

+0

ありがとうございました。 – AB1209

2

この

public class MyClass extends RelativeLayout 

{ LayoutInflaterインフレータ= nullのようにコードを変更してください。 EditText edit_text; ボタンbtn_clear;

public MyClass(Context context, AttributeSet attrs, int defStyle) 
{ 
    super(context, attrs, defStyle); 
    // TODO Auto-generated constructor stub 
    init(); 
} 
    public MyClass(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
     init(); 


    } 

    public MyClass(Context context) 
    { 
     super(context); 
     // TODO Auto-generated constructor stub 
     init(); 
    } 
    public void init() { 
     inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.my_class_layout, this, true); 
     edit_text = (EditText) findViewById(R.id.clearable_edit); 
     btn_clear = (Button) findViewById(R.id.clearable_button_clear); 
     btn_clear.setVisibility(RelativeLayout.INVISIBLE); 
    } 

}

+0

サンディーはすべてをクリアしました。 – AB1209

関連する問題