扩展视图无法正常工作

时间:2012-01-19 05:11:47

标签: android xml android-view android-inflate

我已经用它的所有构造函数扩展了一个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 个答案:

答案 0 :(得分:2)

请更改您的代码

public class MyClass extends RelativeLayout

{        LayoutInflater inflater = 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);
   }

}

答案 1 :(得分:2)

这没关系。 我们在xml中使用View

public MyClass(Context context, AttributeSet attrs)

将调用此构造函数。

因为XML中的所有属性都使用AttributeSet传递给此构造函数,所以这些值将对View的布局和其他字段产生影响。

但是,如果你想在Java中使用你的View,你也应该在所有View的构造函数中膨胀(推荐的方法)。

相关问题