0

我有一个创建视图子对象的活动类

public class DaDActivity extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(new DragSurface(this));
   }
}

这是我的 DragSurface.java

public class DragSurface extends View{

   public DragSurface(Context context) {
        super(context);
        setFocusable(true);

   }
}

而且我有dragsurface.xml文件,但我不知道如何在我的DragSurface.java中使用这个xml文件。我的意思是我希望DragSurface.java在dragsurface.xml中使用背景、按钮或类似的东西,但我无法链接。 java到.xml

感谢帮助..

4

1 回答 1

0
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view  = inflater.inflate(R.layout.yourlayout,null);

尝试上面的行来扩充您的 xml 文件。试试下面的代码:::

public class DaDActivity extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(new DragSurface(this).createView());
   }
}

这是我的 DragSurface.java

public class DragSurface extends View{

   public DragSurface(Context context) {
        super(context);
        setFocusable(true);
   }
   public View createView(){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view  = inflater.inflate(R.layout.yourlayout,null);
        return view; 
     }
}
于 2012-04-07T18:16:07.170 回答