我有一个创建视图子对象的活动类
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
感谢您的帮助..
答案 0 :(得分: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;
}
}