Расширенное представление не работает должным образом - PullRequest
0 голосов
/ 19 января 2012

Я расширил 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 ]

2 голосов
/ 19 января 2012

Это нормально. Когда мы используем View в XML

public MyClass(Context context, AttributeSet attrs)

этот конструктор будет вызван.

Поскольку все атрибуты из XML передаются в этот конструктор с помощью AttributeSet, поэтому эти значения будут влиять на макет View и другие поля.

Однако, если вы хотите использовать View также и в Java, вы должны также раздувать во всех конструкторах View ( Какой рекомендуемый подход ).

2 голосов
/ 19 января 2012

Пожалуйста, измените ваш код следующим образом

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

}

...