java - 如何在 Android 上的现有 XML 布局中包含自定义 View ?

标签 java android xml view

我遵循了本教程( http://stuffthathappens.com/blog/2008/11/13/android-animation-101/ ),并在 Canvas 上绘制了漂亮的旋转文本。当我的主要 Activity 使用时一切正常

setContentView(new SplashScreenAnimation(this));

这是 SplashScreenAnimation:

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);
    }
}

但是我想组合这个动画文本并在其下面有两个按钮,这样我就有一个相对布局:

<?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>

如何将此自定义 View 添加到上面的 LinearLayout 中? (topLinearLayout - 这是最后一个)

我尝试了很多不同的方法,但总是以强制关闭告终。

我主要尝试过这种方法。我试过给它充气等等。

LinearLayout item = (LinearLayout)findViewById(R.id.topLinearLayout);
SplashScreenAnimation child = new SplashScreenAnimation(this);
item.addView(child);

我添加了整个 SplashScreenAnimation 类代码:

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

它有效!!

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

最佳答案

尝试使用此行添加 View :

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

关于java - 如何在 Android 上的现有 XML 布局中包含自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7472644/

相关文章:

java - Selenium 网络驱动程序 : How to check if a ui element exists on specific screen regardless of if it currently visible/hittable

java - 在 java 中执行 javascript - 打开 URL 并获取链接

java - 带有spring boot的多模块项目

java - JNLP 属性中的百分比使 System.getProperty 返回 null

xml - XSLT 复制 - 从 1 个节点创建 2 个节点

java - : ${somevalue} 这是什么意思

java - 在 pom.xml 文件并行测试中需要帮助

android - AsyncTask DoInBackGround 返回类型

android - 使用 React Native 钩子(Hook)

android - 在同一行对齐 textview 和微调器