我有一个从View类扩展的抽象类,以及4个子类来执行一系列矢量绘图。然后我有一个带有四个按钮和一个视图的布局来显示图纸。根据单击的按钮,应在该视图中绘制不同的图形。问题是当我点击一个按钮时,绘制的图形填满整个屏幕,按钮消失。怎么可以解决这个问题?
public abstract class DrawFiguresWithDimensions extends View{
public DrawingFiguresWithDimensions(Context context)
//...
}
public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {..}
public class DrawTWithDimensions extends DrawFiguresWithDimensions {..}
public class DrawDobleTWithDimensions extends DrawFiguresWithDimensions {..}
public class DrawBoxWithDimensions extends DrawFiguresWithDimensions {..}
这是“activity_main.xml”:
<LinearLayout
android:id="@+id/mainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="10"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4"
>
<Button
android:id="@+id/btnSecRec"
android:text="Rec"
android:onClick="btnSecRec"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnSecT"
android:text="T"
android:onClick="btnSecT"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnSecDobleT"
android:text="DT"
android:onClick="btnSecDobleT"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnSecCajon"
android:text="Box"
android:onClick="btnSecCajon"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<View class="com.tari.drawfigureswithdimensions.DrawFiguresWithDimensions"
android:id="@+id/viewDatosSec"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
“MainActivity.java”
public class MainActivity extends Activity {
DrawRectangleWithDimensions sec1;
DrawTWithDimensions sec2;
DrawDoubleTWithDimensions sec3;
DrawBoxWithDimensions sec4;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnSecRec(View view){
sec1 = new DrawRectangleWithDimensions(this);
sec1.setFigure(10, 40);
setContentView(sec1);
}
public void btnSecT(View view){
sec2 = new DrawTWithDimensions(this);
sec2.setFigure(100, 20, 20, 70);
setContentView(sec2);
}
public void btnSecDobleT(View view){
sec3 = new DrawDoubleTWithDimensions(this);
sec3.setFigure(100, 20, 20, 90, 50, 25);
setContentView(sec3);
}
public void btnSecCajon(View view){
sec4 = new DrawBoxWithDimensions(this);
sec4.setFigure(100, 20, 20, 80);
setContentView(sec4);
}
答案 0 :(得分:0)
您不能期望通过在xml布局中引用它来实例化抽象类。
您需要指定具体的实施方案才能使其正常运作。
因此,您可以在布局中提供某种类型的ViewGroup,以作为您希望根据按下的按钮显示的任何具体DrawFiguresWithDimensions实现的父级。
然后,动态删除与您的父ViewGroup关联的当前子项,并添加与您按下的按钮对应的子项。
基本上,您希望在activity_main.xml布局中替换它:
<View class="com.tari.drawfigureswithdimensions.DrawFiguresWithDimensions"
android:id="@+id/viewDatosSec"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
用这个:
<RelativeLayout
android:id="@+id/viewDatosSec"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
然后在处理按钮点击时,执行以下操作:
@Override
public void onClick(View v)
{
int id = v.getId();
if (id == R.id.btnSecCajon)
{
RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
parent.removeAllViews();
parent.addView(new DrawBoxWithDimensions(this));
}
}
答案 1 :(得分:0)
只要加载布局,您的自定义视图就会实例化,因此不允许抽象类!
这是一个在xml中包含自定义视图的示例。 http://developer.android.com/training/custom-views/create-view.html