簡體   English   中英

如何在Android上的現有XML布局中包含自定義視圖?

[英]How can I include a custom view in an existing XML layout on Android?

我已按照本教程( http://stuffthathappens.com/blog/2008/11/13/android-animation-101/ )進行操作,並在畫布上繪制了漂亮的旋轉文字。 當我的主要活動使用時,一切正常

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

但是我想組合這個動畫文本並在其下面有兩個按鈕,所以我有一個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>

如何將此自定義視圖添加到上面的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開始有關嗎?

您應該能夠像在布局中的任何其他視圖一樣包含它。

請嘗試以下操作,然后將內容視圖設置為整個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>

這很容易,您只需要包含擴展View類的類的完整程序包名稱即可:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM