Java 不使用XML创建自己的自定义视图
我想创建一个自定义视图,其中包含两个按钮,而不使用xml 我试过这个:Java 不使用XML创建自己的自定义视图,java,android,xml,view,Java,Android,Xml,View,我想创建一个自定义视图,其中包含两个按钮,而不使用xml 我试过这个: public class ZoomPlate extends LinearLayout { private Context context; private Button plus; private Button minus; public ZoomPlate(Context context) { super(context); init(context)
public class ZoomPlate extends LinearLayout {
private Context context;
private Button plus;
private Button minus;
public ZoomPlate(Context context) {
super(context);
init(context);
}
public ZoomPlate(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
this.context = context;
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0);
plus = new Button(context);
plus.setText("Plus");
plus.setLayoutParams(params);
minus = new Button(context);
minus.setText("Minus");
minus.setLayoutParams(params);
setOrientation(LinearLayout.VERTICAL);
addView(plus);
addView(minus);
}
}
因此,我可以在XML中使用ZoomPlate,如按钮或文本视图,如:
<at.bartinger.zoomplate.ZoomPlate
android:id="@+id/zoomplate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
但是当我尝试此操作时,什么都不会显示您如何将自定义视图添加到XML布局中?您需要使用完整的包名,如
如何将自定义视图添加到XML布局中?您需要使用完整的包名,如
中所示。它可能与在主要活动中设置为内容视图的内容有关?请点击此处:
基本上,您必须从版面资源或视图中进行设置。这可能与在主要活动中设置为内容视图的内容有关?请点击此处:
基本上,您必须从布局资源或视图进行设置。是的,我知道如何用xml编写。问题是Viewobjectyes,我知道如何用xml编写它。问题在于Viewobject