不使用XML创建自定义视图

3
我希望创建一个自定义视图,其中包含两个按钮,但不使用XML。
我尝试了以下代码:
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" />

但是当我尝试这样做时,什么也没有显示出来。
2个回答

0
你是如何将自定义视图添加到XML布局中的?你需要使用完整的包名,例如:<com.blabla.bla.ViewName android:properties="whatever" />

是的,我知道如何在XML中编写它。问题在于Viewobject。 - Dominic

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接