如何在嵌套布局中显示自定义View类?

时间:2018-12-21 08:23:28

标签: java android

我制作了一个自定义类,该类从View public class MyView extends View扩展,它绘制了一个数学函数。我希望将其绘制在具有两个/嵌套(不知道哪个术语更准确)布局的活动中。

我知道如何在简单的空活动中显示它:

public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(myView(this)); //draws function on entire screen
   }
}

但是要在活动中显示哪种XML如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
  <ConstraintLayout>

    //draw my function here

  </ConstraintLayout>
  <ConstraintLayout>
  </ConstraintLayout>
</LinearLayout>

我不知道:(也许我的方法是错误的?请帮忙

关于 Eixx的答案,我收到了错误消息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pwtafunc.marko.pwtafunc/com.pwtafunc.marko.pwtafunc.DrawFuncActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class com.pwtafunc.marko.pwtafunc.Func
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class com.pwtafunc.marko.pwtafunc.Func
     Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class com.pwtafunc.marko.pwtafunc.Func
     Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
        at java.lang.Class.getConstructor0(Class.java:2204)
        at java.lang.Class.getConstructor(Class.java:1683)
        at android.view.LayoutInflater.createView(LayoutInflater.java:618)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.pwtafunc.marko.pwtafunc.DrawFuncActivity.onCreate(DrawFuncActivity.java:16)
        at android.app.Activity.performCreate(Activity.java:6662)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

关于克林巴生的内容,对不起,我的错是NullPointerException,但是当我处理该功能时,还是会在整个屏幕上绘制:(

Marcin Grabowski 解决方案是正确的,非常感谢。

3 个答案:

答案 0 :(得分:2)

您可以将其插入XML,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
  <ConstraintLayout>

    <com.example.MyView/>

  </ConstraintLayout>
  <ConstraintLayout>
  </ConstraintLayout>
</LinearLayout>

答案 1 :(得分:0)

加上Eixx的答案。

要允许直接在XML布局文件中使用视图,则至少需要实现构造器

    public MyView(Context context, AttributeSet attrs)

答案 2 :(得分:0)

您也可以从代码中添加它

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ConstraintLayout constraintLayout = findViewById(R.id.your_constraint_layout_id);
        constraintLayout.addView(myView(this));
    }
}