0

すべてのコンストラクターで RelativeLayout を拡張しました。また、クラスのコンストラクターの 1 つで膨らませている 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) でクラスレイアウトを膨らませると、機能しません。

何が問題なのかわかりません。あなたが私の質問を理解したことを願っています。

4

2 に答える 2

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

}

于 2012-01-19T05:30:25.577 に答える
2

これで結構です。Viewxml でa を使用する場合

public MyClass(Context context, AttributeSet attrs)

このコンストラクタが呼び出されます。

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

ただしView、Java でも使用する場合は、 allViewのコンストラクターでもインフレートする必要があります (これは推奨されるアプローチです)。

于 2012-01-19T05:31:03.493 に答える