【问题标题】:Android - Add a custom view to a component in a xml layoutAndroid - 将自定义视图添加到 xml 布局中的组件
【发布时间】:2015-02-18 22:49:00
【问题描述】:

我有一个从 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);
}

【问题讨论】:

    标签: android xml view


    【解决方案1】:

    您不能期望通过在 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));
        }
    }
    

    【讨论】:

    • 感谢您的评论,迈克尔。我在布局中创建了一个带有四个 DrawFiguresWithDimensions 视图的 ViewGroup,但我仍然停留在如何动态地实现代码上。你能告诉我怎么做吗,例如,在 public void btnSecRec(View view){ } 中,使用 DrawRectangleWithDimensions?
    • @David 我刚刚更新了我的答案,提供了有关如何在父 ViewGroup 中动态放置视图的更多信息。
    • 谢谢迈克尔!解决了这个问题!感谢您如此具体,这对像我这样的初学者非常有用。
    • 当然 - 很高兴为您提供帮助。你介意接受我的回答吗?谢谢。
    【解决方案2】:

    您的自定义视图将在布局加载后立即实例化,因此不允许使用抽象类!

    这里是在您的 xml 中包含自定义视图的示例。 http://developer.android.com/training/custom-views/create-view.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多