Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android上的现有XML布局中包含自定义视图?_Java_Android_Xml_View - Fatal编程技术网

Java 如何在Android上的现有XML布局中包含自定义视图?

Java 如何在Android上的现有XML布局中包含自定义视图?,java,android,xml,view,Java,Android,Xml,View,我遵循了这个教程()并在画布上绘制了一个漂亮的旋转文本。当我的主要活动使用 setContentView(new SplashScreenAnimation(this)); 以下是动画: public class SplashScreenAnimation extends View { private static final String QUOTE = "Nobody uses Java anymore. It's this big heavyweight ball and cha

我遵循了这个教程()并在画布上绘制了一个漂亮的旋转文本。当我的主要活动使用

setContentView(new SplashScreenAnimation(this));
以下是动画:

public class SplashScreenAnimation extends View {
    private static final String QUOTE = "Nobody uses Java anymore. It's this big heavyweight ball and chain.";

    private Animation anim;

    public SplashScreenAnimation(Context context) {
        super(context);
    }

    private void createAnim(Canvas canvas) {
        anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
                .getHeight() / 2);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(10000L);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());

        startAnimation(anim);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // Creates the animation the first time
        if (anim == null) {
            createAnim(canvas);
        }

        Path circle = new Path();

        int centerX = canvas.getWidth() / 2;
        int centerY = canvas.getHeight() / 2;
        int r = Math.min(centerX, centerY);

        circle.addCircle(centerX, centerY, r, Direction.CW);
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setTextSize(30);
        paint.setAntiAlias(true);

        canvas.drawTextOnPath(QUOTE, circle, 0, 30, paint);
    }
}
但是,我想组合此动画文本,并在其下方有两个按钮,因此我有一个RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout1"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:background="@drawable/splash_screen">
 <LinearLayout android:orientation="vertical"
  android:layout_height="wrap_content" android:layout_width="fill_parent"
  android:layout_alignParentBottom="true" android:id="@+id/bottomLinearLayout">
  <Button android:layout_gravity="center_horizontal" android:id="@+id/playButton"
   android:text="Button" android:layout_height="wrap_content"
   android:layout_width="wrap_content"></Button>
  <Button android:layout_height="wrap_content"
   android:layout_width="wrap_content" android:id="@+id/optionsButton"
   android:text="Button" android:layout_gravity="center_horizontal"
   android:layout_marginBottom="30sp"></Button>
 </LinearLayout>


 <LinearLayout android:id="@+id/topLinearLayout"
  android:orientation="vertical" android:layout_height="wrap_content"
  android:layout_alignParentTop="true" android:layout_width="fill_parent">
 </LinearLayout>

</RelativeLayout>
我添加了整个SplashScreenAnimation类代码:

((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this))
它起作用了


为什么其他手动添加的方法不起作用?这与动画需要通过onDraw启动这一事实有关吗?

您应该能够像布局中的任何其他视图一样包含它

尝试以下操作,然后将内容视图设置为整个XML布局:

<LinearLayout android:id="@+id/topLinearLayout"
  android:orientation="vertical" android:layout_height="wrap_content"
  android:layout_alignParentTop="true" android:layout_width="fill_parent">
      <com.YOURPACKAGENAME.SplashScreenAnimation
           android:id="@+id/animation"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"/>
 </LinearLayout>

这非常简单,您只需要包含扩展视图类的类的完整包名:

<LinearLayout
    android:id="@+id/topLinearLayout"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_width="fill_parent">

       <mypackage.SplashScreenAnimation
           android:layout_height="wrap_content"
           android:layout_width="fill_parent"/>

</LinearLayout>

尝试此行添加视图:

((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this))

你试过什么?您是否尝试过
((LinearLayout)findViewById(R.id.topLinearLayout))。添加视图(新的SplashScreenAnimation(this))
欢迎光临。。我应该加上它作为答案吗?你接受吗?每个人都快乐吗派斯,请加上它作为答案。奇怪的是,当我清除了一堆代码并使用了我在上面指定的方法时,它工作了。。所以我猜发生了一些有趣的事情。谢谢:)如果它是一个SurfaceView,这种方法会起作用吗?
((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this))