我希望创建一个自定义视图,其中包含两个按钮,但不使用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" />
但是当我尝试这样做时,什么也没有显示出来。