【发布时间】:2010-10-04 22:40:39
【问题描述】:
我想创建一个包含两个按钮而不使用 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,就像一个按钮或一个 textview 一样:
<at.bartinger.zoomplate.ZoomPlate
android:id="@+id/zoomplate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
但是当我尝试这个时,什么都没有显示
【问题讨论】: