2012-01-19 173 views
0

我已經使用它的所有構造函數擴展了RelativeLayout。並且我在xml中創建了一個佈局,我正在爲我的類的構造函數之一進行膨脹。擴展視圖無法正常工作

我對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 
     } 
    } 

我的客艙佈局代碼,我膨脹

<?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> 

而且我用MyClass的在我的活動的佈局像這樣

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

這工作得很好。 但是當我在構造函數MyClass(Context context)中膨脹我的類佈局時,它不起作用。

我沒有得到什麼問題。我希望你有我的問題。

回答

2

這是可行的。 當我們在XML

public MyClass(Context context, AttributeSet attrs) 

使用View此構造函數將被調用。

由於XML中的所有屬性都使用AttributeSet傳遞給此構造函數,因此這些值將對View的佈局和其他字段有影響。

但是如果你想要在Java中使用您的View也,你也應該在充氣所有View的構造(這是推薦的方法)。

+0

謝謝你解決了我的疑問。 – AB1209

2

請更改喜歡你這個代碼

public class MyClass extends RelativeLayout 

{ LayoutInflater吹氣= NULL; EditText edit_text; Button 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